Reputation: 51
I want to search thorough a vector to see if an element appears. Once this element appears along with others, i want to re-search through the original to see if this second element appears else where within the vector. The end result should show the first element being found and then the information of where the second element appears.
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();
}
}
This gets the error code
error C2678: binary
'=='
: no operator found which takes a left-hand operand of typestd::basic_string<char,std::char_traits<char>,std::allocator<char>>
(or there is no acceptable conversion)
I think it may be because
vector<string> = newband
but that is the only way i can think of to pass the vector information over is to another vector
Upvotes: 2
Views: 112
Reputation: 123450
std::find_first_of
does what you are looking for:
Searches the range [first, last) for any of the elements in the range [s_first, s_last).
Example (also taken from 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"; } }
Upvotes: 3