我想搜索整个矢量,以查看是否有元素出现。一旦该元素与其他元素一起出现,我想重新搜索原始元素,以查看第二个元素是否出现在向量中的其他位置。最终结果应显示找到的第一个元素,然后显示第二个元素出现的位置的信息。
void searchband()
{
ifstream artist("newartist.txt");
string SBand;
cout << "Please enter the band you want to seach" << endl;
cin >> SBand;
system("CLS");
while (artist >> forname >> surname >> bandnum)
{
band.clear();
for (int i = 0; i < bandnum; i++)
{
string tmp;
artist >> tmp;
band.push_back(tmp);
}
artist >> role;
if (find(band.begin(), band.end(), SBand) != band.end())
{
cout << forname << " " << surname << endl;
cout << "Played for: ";
ostream_iterator<string> output_iterator(cout, " ");
copy(band.begin(), band.end(), output_iterator);
cout << " " << endl;
cout << " " << endl;
newband = band;
}
if (find(band.begin(), band.end(), newband) != band.end())
{
cout << forname << " " << surname << endl;
cout << "Played for: ";
ostream_iterator<string> output_iterator(cout, " ");
copy(band.begin(), band.end(), output_iterator);
cout << " " << endl;
cout << " " << endl;
}
system("pause");
cin.get();
main();
}
}
这将获得错误代码
错误C2678:二进制
'=='
:未找到采用std::basic_string<char,std::char_traits<char>,std::allocator<char>>
类型的左操作数的运算符(或没有可接受的转换)
我认为可能是因为
vector<string> = newband
但这是我想到传递向量信息的唯一方法是传递给另一个向量
答案 0 :(得分:3)
std::find_first_of
完成您想要的工作:
在范围[first,last)中搜索范围[s_first,s_last)中的任何元素。
示例(也摘自cppreference):
for (i = 0; i < num_of_friends; i++) { printf("Enter name of friend %d: ", i + 1); fgets(names[i], DEFAULT, stdin); // Removing the \n from the end of the string names[i][strlen(names[i]) - 1] = '\0'; }