Thread: Set insert not working

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

    Set insert not working

    When inserting elements in a set it should only insert unique elements but I get duplicates too.

    What am I doing wrong?

    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;
        std::set<char*> myset;
    
        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);
    
        myset.insert(v.begin(), v.end());
    
        std::set<char*>::iterator it2;
        std::cout << "myset contains: \n";
        for (it2=myset.begin(); it2!=myset.end(); ++it2)
            std::cout << *it2 << '\n';
        std::cout << '\n';
    
        cout << myset.size() << '\n';
        return 0;
    }
    Using Windows 10 with Code Blocks and MingW.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use std::string instead of null terminated C-style strings.
    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
    Of course. Thanks!
    Using Windows 10 with Code Blocks and MingW.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to stop using char* and wchar_t*. Use the standard library strings. C-strings are evil™ in many ways.
    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. Replies: 1
    Last Post: 04-16-2011, 04:44 AM
  2. Map insert not working?
    By JMK in forum C++ Programming
    Replies: 7
    Last Post: 09-28-2010, 01:20 AM
  3. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  4. insert another exe into exe
    By lliero in forum C Programming
    Replies: 8
    Last Post: 04-12-2002, 12:22 PM
  5. Replies: 1
    Last Post: 09-17-2001, 05:46 AM