Question re: iterators...ASAP...please...

Posted on 2004-10-18
Last Modified: 2013-12-14
Do I need to use iterators when comparing strings within vectors?
I need to compare strings from one vector to strings in another vector.  The size of the vectors are different.
Question by:jewee

Accepted Solution

pankajtiwary earned 300 total points
ID: 12343037
Hi jewee,

Do not confuse. Iterators are objects to iterate through the elements of a container ( e.g vector ), if you want to iterate through a vector and so *something* you need iterators. Do check this example out.

// Code not tested
#include <iostream>

using namespace std ;

int main() {
    vector v1<string>(2);
    vector v2<string>(4);



    for ( vector<string>::const_iterator it1 = v1.begin(); it1 != v1.end(); it1++) {
        for ( vector<string>::const_iterator it2 = v2.begin(); it2 != v2.end(); it2++) {
            if ( *it1 == *it2 )
    return 0 ;

Hope this helps.

LVL 86

Assisted Solution

jkr earned 100 total points
ID: 12343424
Ooops, haven't seen that you opened a seperate Q - here's the code again I posted in the last one:

string str = "substring";
vector<string> vs;

for ( vector<string>::iterator i = vs.begin(); i != vs.end(); ++i) {

    if ( -1 != i->find(str)) {

        cout << "Found " << str->c_str() " in in " i->c_str() << endl;

If you want to find the exact match, use

if ( *i == str )

like pankajtiwary already suggested.

Assisted Solution

gugario earned 50 total points
ID: 12343568
There's two ways you can access elements in a vector... one is using the bracket operator, so if you have...

vector<string> a;
vector<string> b;

you could do

if (a[0] == b[1]) cout << "equal";

another is dereferencing an iterator.. so you could have iteractors to the vectors, like

vector<string>::iterator t1 = a.begin();
vector<string>::iterator t2 = b.begin();

if (*t1 == *t2) cout << "equal";

bracket operators, in my opinion, are easier to use.  and part of the reason you would use a vector is so that you could do the direct access with the brackets..

iterators are good i guess if you need to iterate through both vectors completely... but you could still do it with for loops.

hope that helps.


Expert Comment

ID: 12344692
if you like to use bracket operators like Gustavo, vect.size() is the upper bound.
LVL 17

Assisted Solution

rstaveley earned 50 total points
ID: 12348514
There are a lot of nice algorithms you can use that do the iterator work for you.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

typedef std::vector<std::string> StringVector;

class match {
      const std::string &name;
      const StringVector& cmp;
      match(const StringVector& cmp,const std::string& name) : cmp(cmp),name(name) {}
      void operator()(const std::string& str) const
            bool found = find(cmp.begin(),cmp.end(),str) != cmp.end();
            std::cout << str << (found?" found ":" not found ") << "in " << name << '\n';

int main()

      StringVector v1;

      StringVector v2;


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C language IDE – Compilers installation 14 66
What is Codeanywhere used for and can EE provide example of use 9 196
Unable to start eclipse ? 17 128
max float value 3 21
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now