Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2004-10-18
5
Medium Priority
?
273 Views
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.
0
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 4

Accepted Solution

by:
pankajtiwary earned 1200 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);

    v1.push_back("abc");
    v1.push_back("def");

    v2.push_back("ghi");
    v2.push_back("jkl");
    v2.push_back("abc");
    v2.push_back("def");

    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 )
                doSomething();
        }
    }
    return 0 ;
}

Hope this helps.

Cheers!
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 400 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.
0
 
LVL 1

Assisted Solution

by:gugario
gugario earned 200 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.

Gustavo
0
 
LVL 9

Expert Comment

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

Assisted Solution

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

e.g.
--------8<--------
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

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

class match {
      const std::string &name;
      const StringVector& cmp;
public:      
      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;
      v1.push_back("abc");
      v1.push_back("def");

      StringVector v2;
      v2.push_back("ghi");
      v2.push_back("jkl");
      v2.push_back("abc");
      v2.push_back("def");

      for_each(v2.begin(),v2.end(),match(v1,"v1"));
}
--------8<--------
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

688 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