Thread: Vector unique not working on char*

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

    Vector unique not working on char*

    Trying to sort out and remove duplicates from a char* vector.

    Sorting working but the duplicates are still there.

    Code:
    #include <iostream>     // cout
    
    #include <algorithm>    // unique, distance
    #include <string>
    #include <iterator>     // std::back_inserter
    #include <set>
    #include <vector>       // vector
    using namespace std;
     
    
    bool strcmpFunc(const char *c1, const char *c2)
    {
        return strcmp(c1, c2) < 0;
    }
     
    char *url[]=
    {
        "bbbb",
        "bbbb",
        "aaa",
        "ccccccc",
        "a",
        "aaa"
    };
     
    int main ()
    {
        vector <char*> v;
        vector<char*>::iterator it;
    
        int size =  sizeof(url)/sizeof(url[0]);
    
        for (int i=0; i< size; ++i)
            v.push_back(url[i]);
    
        cout << v.size()  <<'\n';
    
        sort(v.begin(), v.end(), strcmpFunc);
        v.erase(std::unique(v.begin(), v.end()), v.end());
    
        cout << "vector after sorting:\n\n";
        for (it=v.begin(); it!=v.end(); ++it)
            cout << *it << '\n';
        cout << '\n';    
    
    return 0;
    }
    Last edited by Ducky; 07-25-2014 at 09:40 AM.
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I will start with the same advice as in your other recent thread: use std::string.

    That said, the problem is that you are using the version of std::unique that compares elements using operator==, hence you are comparing pointers, not strings. You should write another predicate that compares null terminated C-style strings with strcmp, then pass that to std::unique.

    But using std::string would be better.
    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

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    932
    Yes, it's true!

    Again I fell upon a code with windows API in it that took wchar_t* as parameter and I didn't think of rewriting it with std::wstring.

    But now I will do it. Thanks Laserlight!
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    C++ has many ways to interface with C. For strings, you can simply use .c_str(). Don't fall back on using C constructs simply because working with some C stuff. That will hurt you in the end.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector<vector<const char*>> behaving strangely
    By unixmania in forum C++ Programming
    Replies: 6
    Last Post: 07-26-2013, 02:50 PM
  2. Copying string vector to char vector
    By Ducky in forum C++ Programming
    Replies: 21
    Last Post: 07-10-2013, 11:22 AM
  3. Replies: 9
    Last Post: 11-18-2008, 02:59 AM
  4. Replies: 9
    Last Post: 04-04-2008, 12:41 PM
  5. connect vector<int> and vector<char>
    By hdragon in forum C++ Programming
    Replies: 1
    Last Post: 04-27-2006, 01:16 PM