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])'
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Anmol
  • 91
  • 1
  • 3
  • 10
  • 2
    You're missing a `#include `. Also, [get rid of `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Sam Varshavchik Oct 25 '16 at 02:08
  • @SamVarshavchik is right. Please stop using `using namespace std;` – amanuel2 Oct 25 '16 at 02:09
  • 2
    You need to include `` 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 . – Trevor Hickey Oct 25 '16 at 02: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();...)` – 3Dave Oct 25 '16 at 02:12
  • so is not same as ? and why stop using std namespace ? – Anmol Oct 25 '16 at 02:14
  • is for the C library 'str*' functions. is for the C++ std library string functions. Get rid of the namespace so that you are clearly using std::vector and friends. – Jim Buck Oct 25 '16 at 02:43

2 Answers2

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 << " ";
    }
    
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
4

Adding:

#include <algorithm>

solved my problem

Scott
  • 4,974
  • 6
  • 35
  • 62