5

I was trying to use vectors as I just started studying but was stuck on this error. I tried to look cpp reference and wrote as on it but still getting error.

 #include<vector>
 #include<iostream>
 #include<cstring>
 using namespace std;
 int main()
{

vector<string> vec;

vec.push_back("First");

vec.push_back("second");

for( int i = 0; i < 4 ; i++ )
    vec.push_back("RepeatTimes");

vector<string>::iterator fp = find(vec.begin(), vec.end(), "First");


for( vector<string>::iterator it = vec.begin(); it != vec.end(); it++ )                  cout<<*it<<" ";

}

Error is :

[Error] no matching function for call to 'find(std::vector<std::basic_string<char> >::iterator, std::vector<std::basic_string<char> >::iterator, const char [6])'
6
  • 2
    You're missing a #include <string>. Also, get rid of using namespace std;. Commented Oct 25, 2016 at 2:08
  • @SamVarshavchik is right. Please stop using using namespace std; Commented Oct 25, 2016 at 2:09
  • 2
    You need to include <algorithm> It's saying it can't find the function; not that it can't find the string. You most likely got std::string indirectly from <iostream>. Commented Oct 25, 2016 at 2:10
  • using namespace std rocks. This isn't in a header - there's no reason to avoid it here. Unless you just like typing std:: for the hell of it. Also, for(auto it = vec.begin();...) Commented Oct 25, 2016 at 2:12
  • so <cstring> is not same as <string>? and why stop using std namespace ? Commented Oct 25, 2016 at 2:14

2 Answers 2

11

You forgot to include the <algorithm> header where std::find lives.

You should also include <string> for access to std::string.
You most likely included <string> indirectly from another header and should not rely on it.


Since you are learning, I'll go further to suggest modern alternatives to your code.

  1. Instead of pushing elements back one at a time,

    std::vector<std::string> vec;
    vec.push_back("First");
    vec.push_back("second");
    

    you can use initialization lists:

    std::vector<std::string> vec {"First", "Second"};
    
  2. Instead of using a for loop to repeatedly add the same element,

    for( int i = 0; i < 4 ; i++ )
        vec.push_back("RepeatTimes");
    

    you can use the insert method:

    vec.insert(vec.end(), 4, "RepeatTimes");
    
  3. Consider deducing type names when they are verbose and aren't adding any readability value to the code:

    auto fp = std::find(vec.begin(), vec.end(), "First");
    
  4. Use range-based for loops when iterating over the entire range of the container:

    for (auto it: vec){
        std::cout << it << " ";
    }
    
Sign up to request clarification or add additional context in comments.

1 Comment

4. An alternative is using std::for_each with lamda function :)
4

Adding:

#include <algorithm>

solved my problem

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.