Thread: Compare elements of string vector

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    932

    Compare elements of string vector

    Hi,

    I'm trying to write a program to compare two lists of names and find the missing ones.
    I thought about to put them in two vectors and compare them one by one.
    This is how I started out and I get and error on the second element of if() statement.
    What am I doing wrong and is there a better method to do this?


    Code:
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        vector<string> myvector1;
        vector<string> myvector2;
    
        myvector1.push_back("aaa");
        myvector1.push_back("bbb");
        myvector1.push_back("ccc");
    
        myvector2.push_back("aaa");
        myvector2.push_back("ddd");
    
        for_each (myvector1.begin(), myvector1.end(), [](string word)
        {
            if(word == myvector2.at(0))
               cout << word << endl;
        });
    
        return 0;
    }
    Last edited by Ducky; 10-06-2012 at 06:22 AM.
    Using Windows 10 with Code Blocks and MingW.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare array elements
    By Mentallic in forum C Programming
    Replies: 5
    Last Post: 08-28-2011, 04:56 AM
  2. How to Compare elements in arrays
    By axe in forum C Programming
    Replies: 13
    Last Post: 11-16-2007, 03:04 AM
  3. Table elements compare...
    By ihtus in forum C++ Programming
    Replies: 1
    Last Post: 04-10-2006, 09:30 AM
  4. How to compare structure elements
    By khpuce in forum C Programming
    Replies: 6
    Last Post: 04-10-2005, 11:40 AM