gpt4 book ai didi

c++ - 在另一个 vector 中搜索一个 vector

转载 作者:搜寻专家 更新时间:2023-10-31 01:26:12 26 4
gpt4 key购买 nike

我想彻底搜索一个 vector ,看看是否出现了一个元素。一旦这个元素与其他元素一起出现,我想重新搜索原始元素以查看第二个元素是否出现在 vector 中的其他位置。最终结果应显示找到的第一个元素,然后显示第二个元素出现位置的信息。

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();

}
}

获取错误码

error C2678: binary '==' : no operator found which takes a left-hand operand of type std::basic_string<char,std::char_traits<char>,std::allocator<char>> (or there is no acceptable conversion)

我想可能是因为

vector<string> = newband

但这是我能想到的将 vector 信息传递给另一个 vector 的唯一方法

最佳答案

std::find_first_of做你正在寻找的:

Searches the range [first, last) for any of the elements in the range [s_first, s_last).

示例(也取自 cppreference ):

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
std::vector<int> v{0, 2, 3, 25, 5};
std::vector<int> t{3, 19, 10, 2};

auto result = std::find_first_of(v.begin(), v.end(), t.begin(), t.end());

if (result == v.end()) {
std::cout << "no elements of v were equal to 3, 19, 10 or 2\n";
} else {
std::cout << "found a match at "
<< std::distance(v.begin(), result) << "\n";
}
}

关于c++ - 在另一个 vector 中搜索一个 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55708743/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com