Thread: Compare elements of string vector

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    930

    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.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Posts
    930
    @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.
    Using Windows 10 with Code Blocks and MingW.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.
    Using Windows 10 with Code Blocks and MingW.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Posts
    930
    Thank you so much!
    Using Windows 10 with Code Blocks and MingW.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.
    Using Windows 10 with Code Blocks and MingW.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.
    Using Windows 10 with Code Blocks and MingW.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    930
    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.
    Using Windows 10 with Code Blocks and MingW.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

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