vectors and iterators

I'm reading through this beginner's guide to c++, and right now I'm on the chapter that explains how to use vectors and iterators. So I'm trying to write a program that puts this stuff into use, and so far I haven't had much success. I'm trying to write code that tests whether the user's entry matches any of the strings in a string vector. So I'm using an iterator with the find() algorithm, like this:

1
2
3
iter=find(choices.begin(), choices.end(), userEntry);
if (iter !=choices.end())
{etc...


This code is successful in testing whether userEntry matches any of the strings in my vector, but I can't figure out how to determine which specific string it matches. I thought I could use iter to return a numeric value that represents the matching string (like, 0 for the first string, 1 for the second string, etc.), but when I tried to use iter as a number, I got compile errors.

I know I could just use a for{} loop to cycle through all the strings to find the matching string, but I really want to learn how to use the find() algorithm, since it seems like it would be simpler.

Thanks.
Something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::vector<std::string> words 
    {
        "chuffed", 
        "apparent", 
        "buckle", 
        "awful", 
        "besides", 
        "check",
    };

    std::string input;

    while (std::cout << "Enter a word\n> " && std::cin >> input && input != "quit")
    {
        auto it = std::find(words.begin(), words.end(), input);

        if (it != words.end())
            std::cout << '"' << input << "\" is string number " << (it - words.begin()) + 1 << '\n';
        else
            std::cout << '"' << input << "\" is not found!\n";
    }
}




http://ideone.com/gw4BLW
Thanks for your reply cire. I tried your code, but I must be doing something else wrong because I'm still getting compile errors. I'm getting this error:

ISO C++ forbids declaration of 'iter' with no type

on the line that starts with "auto" (it's basically the same as the one you wrote above). And then after that, there's an error for every reference to 'iter."

Am I declaring 'iter' wrong? I declared it like this:

 
vector<string>::const_iterator iter;


But I also tried

 
vector<string>::iterator iter;


and even

 
vector<int>::iterator iter;


but it didn't seem to make any difference.

Your suggestions are greatly appreciated!
> ISO C++ forbids declaration of 'iter' with no type
> on the line that starts with "auto"

Type deduction with auto requires C++11. Compile with -std=c++11

Or use exactly the same type that you used earlier in the code that worked. The one that you used in:
1
2
3
> iter=find(choices.begin(), choices.end(), userEntry);
> if (iter !=choices.end())
> {etc...

> This code is successful in testing ...
Thanks JLBorges and cire. I fiddled with my code for a while and I finally got it to work. I'm very new to C++, and I feel like some of these concepts are a little over my head, but your advice was very helpful. Thanks again!
> I'm very new to C++, and I feel like some of these concepts are a little over my head

The concepts are important. Perhaps this tutorial would help:
http://www.mochima.com/tutorials/STL.html


it - words.begin() - we can subtract one iterator from another to get a count of elements between them - because the iterator to the sequence contained in a std::vector<> is a random access iterator.
http://stdcxx.apache.org/doc/stdlibug/2.html
we can subtract one iterator from another to get a count of elements between them - because the iterator to the sequence contained in a std::vector<> is a random access iterator.


Okay, I see. Thanks, that was helpful. I'll check out that link, too.
Topic archived. No new replies allowed.