: 多明尼加共和国dz:阿尔及利亚ec:厄瓜多尔ee:爱沙尼亚例如:埃及呃:西方人 撒哈拉 </跨度> 呃:厄立特里亚es:西班牙et:埃塞俄比亚欧盟:欧盟fi:芬兰fj:斐济fk:福克兰
countries 是空的矢量,使用 push_back 添加新项目:
countries
push_back
while (getline(inFile, line)) //This is the first time I am using getline I suspect I might be misusing it. { //countries[i] = line; countries.push_back(line); i = i + 1; }
另一个问题:
if (line.find(countrycode) != -1)
它应该改为
if (line.find(countrycode) == 0)
因为您希望countrycode位于该行的开头。
std::string 还有许多其他功能,如 find_last_of 和 substr ,使用它们而不是通过字符。
std::string
find_last_of
substr
void findsitetype(string email) { size_t dot = email.find_last_of('.'); if (dot == string::npos || dot == (email.size() - 1)) return; string countrycode = email.substr(dot + 1); if (!countrycode.size()) return; vector<string> countries; if (countrycode == "edu") cout << "Educational institutions"; else if (countrycode == "org") cout << "Not-for-profit organizations"; else if (countrycode == "gov") cout << "Government entities"; else if (countrycode == "mil") cout << "Military installations"; else if (countrycode == "net") cout << "Network service providers"; else if (countrycode == "com") cout << "Commercial ventures"; else if (countrycode.size() == 2) { ifstream inFile("countries.txt"); if (inFile.fail()) { cout << "File not found."; exit(1); } else { string line; while (getline(inFile, line)) countries.push_back(line); inFile.close(); for (size_t j = 0; j < countries.size(); j++) { if (countries[j].find(countrycode) == 0) { line = countries[j]; if (line.size() > 5) cout << "country: " << line.substr(5) << "\n"; } } } } }