Try to find element from array.

I try to find element from string array, but when I use find(cmemb[0], cmemb[19], c) - it brings errors C2675, C2100, C2088. Thank you for help!

1
2
3
4
5
6
7
8
9
10
string cmemb[20];

string get(string b) {

	string c = b.substr(0, 2);
	string t = find(cmemb[0], cmemb[19], c);
	cout << t;

	return b;
};
What you do is equivalent of:
1
2
3
4
5
6
7
8
9
10
11
string get(string b) {
	string a = cmemb[0];
	string b = cmemb[19]
	string c = b.substr(0, 2);
	string t = find( a, b, c );
	cout << t;
	return b;
};
// but std::find is
template<class InputIterator, class T>
  InputIterator find( InputIterator first, InputIterator last, const T& val );

The std::string is not an iterator-like object.

Note that find both requires and returns iterators. Furthermore,
Return value
An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.


You would print the return value even when nothing was found.

Why does the function print anything?
Why does the function return its parameter?


Name of array converts implicitly into pointer to first element of that array.
Pointer to first element plus size of array is pointer one past the last element.
Pointers are iterator-like objects.
1
2
3
4
5
6
string get(string b) {
	string c = b.substr(0, 2);
	auto t = find( cmemb, cmemb+20, c );
	if ( t != cmemb+20 ) cout << *t;
	return "Hello world";
};

Topic archived. No new replies allowed.