Thread: How can i make a unique copy of strings?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    50

    Unhappy How can i make a unique copy of strings?

    i have problems with regards using the unique_copy stl function. it seems to not working his job right. i'm using borland, and what it does is just copy the original vector and not the unique string values. i tried to emulate it on devcpp and it produces an error..

    here's the code:
    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <iostream>
    #include <string>
    
    using namespace std;
      
    int main()
    {
     
    //UNIQUE ELEMENTS
    
    //Initialize two vectors
    vector<string> v, result;
    v.push_back("X");
    v.push_back("X");
    v.push_back("y");
    
    //Create an insert_iterator for results
    insert_iterator<vector<string>> ins(result, result.begin());
    
    //Demonstrate includes
    cout << "The vector: " << endl << "    ";
    for (int i = 0; i < v.size(); i++){
        cout << v[i] << " ";
    }
    
    //Find the unique elements
    unique_copy(v.begin(), v.end(), ins);
    
    //Display the results
    cout << endl << endl
         << "Has the following unique elements:"
         << endl << "     ";
    
    //copy(result.begin(),result.end(),
    for (int i = 0; i < result.size(); i++){
        cout << result[i] << " ";
    }
    
        system("PAUSE");
        
        return 0;
    }
    the error is `result' cannot appear in a constant-expression and points to the insert_iterator. I'm really confused right now, if i had a vector of strings how would i get a copy with only the unique values?

    example

    v[0] = "X"
    v[1] = "X"
    v[2] = "y"

    Result:

    r[0] = "X"
    r[1] = "y"

    thanks for your help!

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The space is important.
    Code:
    insert_iterator<vector<string> > ins(result, result.begin());
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    What the?! you're absolutely right! thanks!! i didn't know that the space is important. hmm... what is the space for? is is for the allocator?

    now, i still have problem with borland's unique_copy but at least i'm one step one solving it

    thanks again!

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The compiler interprets >> as operator>> rather than two consectutive >'s.

    As for Borland, I see what you see but don't have an answer.

    [edit]Perhaps this?
    Code:
       unique_copy(v.begin(), v.end(), 
                   insert_iterator<vector<string> > (result, result.begin()));
    [edit=2]As in,
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <iterator>
    using namespace std;
    
    int main()
    {
       //UNIQUE ELEMENTS
    
       //Initialize two vectors
       vector<string> original, result;
       original.push_back("X");
       original.push_back("X");
       original.push_back("y");
    
       //Demonstrate includes
       cout << "original:\n";
       copy(original.begin(), original.end(), ostream_iterator<string>(cout, "\n"));
    
       //Find the unique elements
       unique_copy(original.begin(), original.end(), 
                   insert_iterator<vector<string> > (result, result.begin()));
    
       //Display the results
       cout << "result:\n";
       copy(result.begin(), result.end(), ostream_iterator<string>(cout, "\n"));
    
       return 0;
    }
    
    /* my output
    original:
    X
    X
    y
    result:
    X
    y
    */
    Last edited by Dave_Sinkula; 01-11-2006 at 11:12 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    i see. thank you for clearing that out. i'm actually doing a hard method of copying the vector to another then doing a unique copy for another vector. your solution is much more concise and straight forward i'll try to use that one...

    the problem on borland is it does not perform a unique_copy.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Both g++ and bc55 produce the same output for me.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    this is my problem:
    Code:
    class A {
    public:
         std::string str;
        std::string& getStr(){
               return str;    
        }
    }
    
    class B{
    public:
        std::vector<A> vector_A;
    }
    
    class C{
    public:
       std::vector<B>vector_B;
    }
    
    int main(){
    //initialize class C.
    C c;
    //Assuming the vectors has values...
    
    std::vector<std::string> vstr, result;
    
    std::vector<B>::iterator = iter_B;
    std::vector<A>::iterator = iter_A;
    
    for (iter_B = c.vector_B.begin(); iter_B < c.vector_B.end(); iter_B++){
        for ( iter_A = iter_B->vector_A.begin(); iter_A < iter_B->vector_A.end(); iter_A++){
               //place to a temporary vector        
               vstr.push_back(iter_A->getStr());
       }
    }
    
    //does not work..
     unique_copy(vstr.begin(), vstr.end(), 
                   insert_iterator<vector<string> > (result, result.begin()));
    
       //Display the results
       cout << "result:\n";
       copy(result.begin(), result.end(), ostream_iterator<string>(cout, "\n"));
    
       return 0;
    }
    assuming all headers are included correctly...

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Can you post the exact code that compiles and gives the wrong output?

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The basic algorithm you are using would rely on the source vector either being sorted, or having all like strings within the source vector always being adjacent to each other. If you don't expect this to be the case, then your destination vector will end up containing duplicates.

    Could you copy from your source vector into a set instead?
    Code:
    set<string> strset;
    vector<string> strvect;
    
    ...
    
    // Copy from vector into set, set will only contain unique elements afterwards
    copy( strvect.begin(), strvect.end(), inserter(strset,strset.begin()) );
    In which case the set ends up with all the unique elements from the source vector.
    Last edited by hk_mp5kpdw; 01-13-2006 at 06:53 AM.
    "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

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    50
    ok thank you for your replies.

    i give up on using unique_copy and use a more borland specific method using TStringList. i guess it's not as good as using the unique_copy but at least it's working for now.
    Last edited by what3v3r; 01-12-2006 at 09:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copy strings from editcontrols
    By Drogin in forum C++ Programming
    Replies: 14
    Last Post: 11-01-2005, 01:55 PM
  2. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  3. copy strings problem
    By dgcampos in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2004, 08:05 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM