push_backを用いるとすればどのようにすればよいでしょうか?
#include <iostream>
#include <vector>
#include <string>
class Entry {
public:
std::string name;
std::string phone;
Entry (const std::string& nm="", const std::string& ph="") {
name = nm;
phone = ph;
}
};
std::ostream& operator<< (std::ostream& os, const Entry& e) {
os << e.name << ": " << e.phone;
return os;
}
int main (void)
{
std::vector<Entry> e(10);
int n = 0;
int number;
//以下のコメントアウトを取れば、求める動作を行う
/*
e[n++] = Entry ("Tokyo Company (PR)", "012-1111-1111");
e[n++] = Entry ("Tokyo Company (KFC)", "012-1111-1111");
e[n++] = Entry ("Osaka City Station", "012-1111-1111");
e[n++] = Entry ("Newtown NT", "012-1111-1111");
e[n++] = Entry ("Newtown Hotel", "012-1111-1111");
*/
for (int i=0; i<n; i++) {
number = e[i].phone.find ("-") + 1;
e[i].phone.replace ( 0, e[i].phone.length(), e[i].phone.substr (number) );
}
std::cout << std::endl;
std::cout << "検索用文字列を入力して下さい: ";
std::string s;
std::cin >> s;
for (int i=0; i<n; i++) {
if ( e[i].name.find(s) != e[i].name.npos ) {
std::cout << e[i] << std::endl;
}
}
return 0;
}