about inset value in vector

This is a discussion on about inset value in vector within the C++ Programming forums, part of the General Programming Boards category; hi everyone this is my solution but there is a problem.. can someone help me on the inserting value into ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    about inset value in vector

    hi everyone this is my solution but there is a problem..

    can someone help me on the inserting value into vector of string

    Code:
    / insert a value into the set
    // Pre: a string value
    // Post: the value is inserted into the set if it 
    //      is not already present
    void StringSet::insert(string value) 
    {
         int contentSize = contents.size();
         
         for(int i = 0; i < 4; i++)
         {
                 if(contents[i] != value)
                      contents.push_back(value);     
         }      
    }
    this is the header file

    Code:
    using namespace std;
    // StringSet
    //  Class holding a set of strings
    class StringSet 
    {
     private: 
    
      vector <string> contents;
     
     public:
    
      // insert a value into the set
      // Pre: a string value
      // Post: the value is inserted into the set if it 
      //      is not already present
      void insert(string value);
      
      // test for a value
      // Pre: a string value
      // Post: returns 1 if the value is present in the StringSet, 0 otherwise
      int contains(string value); 
      
    
      
    };
    Last edited by peter_hii; 09-19-2006 at 04:29 AM.

  2. #2
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    So... what exactly is your problem?

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    You are not using contentSize in your loop like it seems you would want to given its presence. You also insert a copy of value each time through the loop if value does not match when you should only be doing the push_back a single time after reaching the conclusion that the vector does not contain the given value. It seems you could use a call to your contains function to see if the string exists and then do a push_back if it does not.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    ok make it simple

    void StringSet::insert(string value)
    {

    }

    wat will you will put on this kinda code.?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,798
    If you've already figured out the contains function then...
    Code:
    void StringSet::insert(string value) 
    {
        if( !contains(value) )
        {
            // Now it's your turn... what would you put here?
        }
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    correct me if i am wrong
    Code:
    void StringSet::insert(string value) 
    {
        if( !contains(value) )
        {
            contents.push_back(value)
        }
    }

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    i use contents because it is being declare in private section of the header file...

    vector <string> contents;

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> correct me if i am wrong
    That looks right.

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    141

    check if vector contain same string?

    is this code correct as i want to check is the vector of string contain the same value...

    Code:
    int StringSet::contains(string value) 
    {   
        int count = 0;
        
        for(int position = 0; position < contents.size(); position++)
        {
                if(contents.at(position) == value)
                {
                     count++;
                }
        }
        if(count >=2)
             return 1;
        else
        {
            return 0;
        }            
    }

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    I am curious as to why you call it "string set" when it's not a std::set.

    In fact, a std::set would have this sort of behavior naturally, although:

    1. A std::set is always sorted, you can't specify the order in which the values should be stored (except insofar as you can make sorting rules).

    2. A std::set does not have random access iterators, meaning you can't easily access, say, the 25th element in the set. You can easily go through the set in order, and you can easily insert or check to see if a value exists in the set, but you can't easily access based on the position of the element in the set.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    hi cat....first of all, that is just a name which i used to insert vector of string...

    from ur reply, u mean use sorting method to sort ? i dont really understand about ur reply... cheers/\.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> is this code correct as i want to check is the vector of string contain the same value...
    That code will return 1 if the vector contains two copies of the string. That may be what you wanted, but with a name like "contains" I would expectit should return 1 if the vector contained just 1 copy of the string.

    Along the same lines as CornedBee's reply- if you aren't doing this as a learning exercise, there is likely other standard functionality that can do what you want that has already been coded. One possibility is the std::set class, although that works a little differently than your current code would (if you don't care what order the strings are in your vector it might be a better alternative). Also, your contains function could just use the find algorithm instead of its own loop.

  13. #13
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by peter_hii
    hi cat....first of all, that is just a name which i used to insert vector of string...

    from ur reply, u mean use sorting method to sort ? i dont really understand about ur reply... cheers/\.
    A std::set will sort all the entries based on operator<, so if you create your own class, you make your own operator< and you can then do whatever you want.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  14. #14
    Registered User
    Join Date
    Jan 2006
    Posts
    141
    Quote Originally Posted by Daved
    >> is this code correct as i want to check is the vector of string contain the same value...
    That code will return 1 if the vector contains two copies of the string. That may be what you wanted, but with a name like "contains" I would expectit should return 1 if the vector contained just 1 copy of the string.

    Along the same lines as CornedBee's reply- if you aren't doing this as a learning exercise, there is likely other standard functionality that can do what you want that has already been coded. One possibility is the std::set class, although that works a little differently than your current code would (if you don't care what order the strings are in your vector it might be a better alternative). Also, your contains function could just use the find algorithm instead of its own loop.

    hi daved, great that you know wat i am up to... yes i just want to find the same value inside the vector

    for example: setstring::contains(string value)

    and in main.cpp i will put as

    setstring setstr;

    setstr.insert("one");
    setstr.insert("two");
    setstrinsert("three");
    setstr.insert("one");

    if ( setstr.contains("one") )
    cout << "ok ";
    else
    cout << "fail ";

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    I still don't understand what you think you want to do, but what you should do is call contains inside the insert function, and if it returns true then "fail", otherwise ok. That would mean that the second insert would fail. In that case you want to check in the contains function if count is greater than or equal to 1 (you could also just return immediately if you find a match to avoid looping through the rest of the words).

    Then in your insert function, if contains returns true then you don't insert. That way you never have more than one copy of the same string in the vector. You don't want to check the contains function after you insert, because at that point there is already a duplicate.
    Last edited by Daved; 09-21-2006 at 09:03 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. syntax help?
    By scoobygoo in forum C++ Programming
    Replies: 1
    Last Post: 08-07-2007, 10:38 AM
  3. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  4. Need some help/advise for Public/Private classes
    By nirali35 in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2006, 12:34 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

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