Compare elements of string vector

This is a discussion on Compare elements of string vector within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to write a program to compare two lists of names and find the missing ones. I thought ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749

    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.
    Compiler MSVC++ 2010 with Code::Blocks.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,675
    I do not know about vectors,but i know that strcmp receives strings and not characters.I think that myvector2.at(0) returns a char

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Obviously you would change

    if(!strcmp(word.c_str(), myvector2.at(0)))
    cout << word << endl;

    to

    if(!word != myvector2[i])
    cout << word << endl;

    Where i is the ith element of myvector2 (yes, you would need a loop for this).
    And while you're at it, why don't you store strings directly in the vector and make the lambda take a const reference?

    EDIT: Original reply wrong, but because the first post was updated, I could not see the original code and fix the error, so take this with a grain of salt.
    Last edited by Elysia; 10-06-2012 at 06:25 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    @std10093
    Hi, no if you cout myvector2.at(0), it gives you the whole string.
    I replaced strcmp with "==" by the way in the code because its a string vector.
    Last edited by Ducky; 10-06-2012 at 06:36 AM.
    Compiler MSVC++ 2010 with Code::Blocks.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Hi and thanks Elysia,

    Code:
    int i = 0; 
    if(word == myvector2[i])
    does not compile either:

    "error C3493: 'myvector2' cannot be implicitly captured because no default capture mode has been specified"

    Sorry for modifying the code I didnt realize the error before.

    What is lambda?
    Last edited by Ducky; 10-06-2012 at 06:34 AM.
    Compiler MSVC++ 2010 with Code::Blocks.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Your lambda is this part:

    Code:
    [](string word)
        {
            if(word == myvector2.at(0))
               cout << word << endl;
        });
    You need to tell the compiler to capture your vector in the lambda for it to work. So:

    Code:
    [&myvector2](string word)
        {
            if(word == myvector2.at(0))
               cout << word << endl;
        });
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Thank you so much!
    Compiler MSVC++ 2010 with Code::Blocks.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    Quote Originally Posted by std10093 View Post
    I do not know about vectors,but i know that strcmp receives strings and not characters.I think that myvector2.at(0) returns a char
    Wrong. myvector2 is a vector of strings. It's elements are strings.

    In response to the original question, you might want to look up std::set_difference .... it's an algorithm in the C++ standard library.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Thanks grumpy,

    Can you help me make set_difference work with strings?
    I get the error: could not deduce template argument for 'const std::vector<_Ty,_Ax> &' from 'std::basic_string<_Elem,_Traits,_Ax>'|

    Code:
    #include <algorithm>
    #include <iterator>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        vector<string> vec1, vec2, result(10);
    
        vec1.push_back("aaa");
        vec1.push_back("bbb");
        vec1.push_back("ccc");
    
        vec2.push_back("aaa");
        vec2.push_back("ddd");
        vec2.push_back("ccc");
    
        sort(vec1.begin(), vec1.begin()+3);
        sort(vec2.begin(), vec2.begin()+3);
    
        std::set_difference(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), result.begin());
    
        for(int i = 0; i< result.size(); i++)
        {
           cout << "differences:\n " << result[i] << "\n";
        }
        return 0;
    }
    Last edited by Ducky; 10-08-2012 at 01:25 AM.
    Compiler MSVC++ 2010 with Code::Blocks.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    I recommend using back_inserter instead of anticipating the number of elements in the result, e.g.,
    Code:
    vector<string> result;
    std::set_difference(vec1.begin(), vec1.end(),
                        vec2.begin(), vec2.end(),
                        back_inserter(result));
    Note that if you don't sort the vectors first, then the result may not be quite what you expect.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Hi Laserlight,

    Thanks for your help.
    I modified the code but still getting the same error.

    Code:
    #include <algorithm>
    #include <iterator>
    #include <vector>
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        vector<string> vec1, vec2, result;
    
        vec1.push_back("aaa");
        vec1.push_back("bbb");
        vec1.push_back("ccc");
    
        vec2.push_back("aaa");
        vec2.push_back("ddd");
        vec2.push_back("ccc");
    
        sort(vec1.begin(), vec1.begin()+3);
        sort(vec2.begin(), vec2.begin()+3);
    
        std::set_difference(vec1.begin(), vec1.end(),
                            vec2.begin(), vec2.end(),
                            back_inserter(result));
    
    
        return 0;
    }
    Last edited by Ducky; 10-08-2012 at 01:36 AM.
    Compiler MSVC++ 2010 with Code::Blocks.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Quote Originally Posted by Ducky
    I modified the code but still getting that error.
    On which line?

    EDIT:
    Oh, you did not #include <string>
    Last edited by laserlight; 10-08-2012 at 01:40 AM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Dec 2007
    Location
    France
    Posts
    749
    Yeah with #include <string> it compiles.
    Its interesting because sometimes you dont need to include <string> if <iostream> is included. Now I understand that it must depend on the functions.
    Anyway now it works, thank you.
    Compiler MSVC++ 2010 with Code::Blocks.

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,424
    Quote Originally Posted by Ducky View Post
    Yeah with #include <string> it compiles.
    Its interesting because sometimes you dont need to include <string> if <iostream> is included. Now I understand that it must depend on the functions.
    That is precisely why you should not rely on such behavior. Always include appropriate headers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,198
    Quote Originally Posted by Ducky View Post
    Its interesting because sometimes you dont need to include <string> if <iostream> is included. Now I understand that it must depend on the functions.
    No, it actually depends on the implementation of the standard library that ships with your compiler.

    The practical rule: if you are using any functions from a standard header file, ensure you #include that header file. Determine that using documentation for the functions you use. Don't rely on your compiler checking such things for you.

    If you get into the habit of relying on any standard header #include'ing another then, eventually, you will encounter problems. Such as your code breaking after updating your compiler/library (that includes applying a service pack or even a simple bug fix). Or, if you need to port your code to a different compiler or host system.
    Right 98% of the time, and don't care about the other 3%.

Page 1 of 2 12 LastLast
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, 02: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21