CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2014
    Posts
    3

    remove duplicated from sorted vector

    Hi,

    I have vector pair like below, it is sorted but I want to remove duplicate values: The vector is like below:

    std::vector<std:air<std::string,double>> fileWeight;

    Here is what in vector:

    scene1.jpg 0.4
    scene2.png 0.4

    scene3.jpg 2.3
    scene4.jpg 4.1

    The highlighted top 2 are duplciated. How can I remove duplciated like this where I have to check whole pair?



    Thanks

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: remove duplicated from sorted vector

    It is not clear how the first two elements are equal. I guess if the second values of the pair are equal ?

    If so...

    Code:
    #include <algorithm>
    
    struct EqualWeight
    {
        bool operator () (const std::pair<std::string,double> & lhs , const std::pair<std::string,double> & rhs)
        {
            return lhs.second == rhs.second;
        }
    };
    
    // usage:
    
    fileWeight.erase( std::unique(fileWeight.begin(),fileWeight.end(),EqualWeight()) , fileWeight.end() );
    or, using a lamda

    Code:
    auto f =  [] (const std::pair<std::string,double> & lhs , const std::pair<std::string,double> & rhs) { return lhs.second== rhs.second; };
    
    fileWeight.erase( std::unique(fileWeight.begin(),fileWeight.end(),f) , fileWeight.end() );
    You don't really need the local variable "f" in the above code, but I think it makes it a little more readable.
    Last edited by Philip Nicoletti; December 1st, 2014 at 08:37 PM.

  3. #3
    Join Date
    Dec 2014
    Posts
    3

    Re: remove duplicated from sorted vector

    I m sorry, I did one mistake. Actually, the similarity is detected by name of file and second value in pair. For name, we have to ignore the type of file. Here is what in vector:

    scene1.jpg 0.4
    scene1.png 0.4

    scene3.jpg 2.3
    scene4.jpg 4.1

    Can u please help me!! pleaseee

    Quote Originally Posted by Philip Nicoletti View Post
    It is not clear how the first two elements are equal. I guess if the second values of the pair are equal ?

    If so...

    Code:
    #include <algorithm>
    
    struct EqualWeight
    {
        bool operator () (const std::pair<std::string,double> & lhs , const std::pair<std::string,double> & rhs)
        {
            return lhs.second == rhs.second;
        }
    };
    
    // usage:
    
    fileWeight.erase( std::unique(fileWeight.begin(),fileWeight.end(),EqualWeight()) , fileWeight.end() );
    or, using a lamda

    Code:
    auto f =  [] (const std::pair<std::string,double> & lhs , const std::pair<std::string,double> & rhs) { return lhs.second== rhs.second; };
    
    fileWeight.erase( std::unique(fileWeight.begin(),fileWeight.end(),f) , fileWeight.end() );
    You don't really need the local variable "f" in the above code, but I think it makes it a little more readable.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: remove duplicated from sorted vector

    Quote Originally Posted by memon421
    I did one mistake. Actually, the similarity is detected by name of file and second value in pair. For name, we have to ignore the type of file.
    Therefore, you should modify the predicate (EqualWeight in Philip Nicoletti's first example; the lambda in the second) to reflect this requirement.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jul 2013
    Posts
    576

    Re: remove duplicated from sorted vector

    Well let's say you have the ability to determine whether two vector items are equal. And let's also say that if two or more items are equal you want to remove all of them.

    Note that items in fileWeight are in sorted order and that means that equal items are grouped next to each other.

    The easiest way is to use a temporary vector. It's initially empty.

    You scan through fileWeight once considering the items one by one. If the current item is equal to the item immediately after it's a duplicate, otherwise not. If it is a duplicate you skip all items after the current item that are equal to the current item. If it's not a duplicate you instead copy the current item to the temporary vector. Then you continue with the next item.

    Afterwards no item in the temporary vector is a duplicate. You clear fileWeight and add all items of the temporary vector to fileWeight.
    Last edited by razzle; December 2nd, 2014 at 11:46 PM.

  6. #6
    Join Date
    Dec 2014
    Posts
    3

    Re: remove duplicated from sorted vector

    Thanks

    Quote Originally Posted by razzle View Post
    Well let's say you have the ability to determine whether two vector items are equal. And let's also say that if two or more items are equal you want to remove all of them.

    Note that items in fileWeight are in sorted order and that means that equal items are grouped next to each other.

    The easiest way is to use a temporary vector. It's initially empty.

    You scan through fileWeight once considering the items one by one. If the current item is equal to the item immediately after it's a duplicate, otherwise not. If it is a duplicate you skip all items after the current item the are equal to the current item. If it's not a duplicate you instead copy the current item to the temporary vector. Then you continue with the next item.

    Afterwards no item in the temporary vector is a duplicate. You clear fileWeight and add all items of the temporary vector to fileWeight.

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: remove duplicated from sorted vector

    Quote Originally Posted by razzle View Post
    You scan through fileWeight ...
    Are you suggesting to re-implement std::unique?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Jul 2013
    Posts
    576

    Re: remove duplicated from sorted vector

    Quote Originally Posted by VladimirF View Post
    Are you suggesting to re-implement std::unique?
    You must learn to walk before you can run. I would only suggest a function to someone who already knew how to do it without the function. And the OP obviously didn't since he had to ask.

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: remove duplicated from sorted vector

    Quote Originally Posted by VladimirF
    Are you suggesting to re-implement std::unique?
    It looks like memon421 has never implemented an algorithm to do this and needs the practice, so re-implementing std::unique would be a good idea at some point.

    Quote Originally Posted by razzle
    You must learn to walk before you can run. I would only suggest a function to someone who already knew how to do it without the function.
    On the other hand, it may be good to learn how to use std::unique and then implement it, similiar to the learning method suggested by Stroustrup in his essay on Learning Standard C++ as a New Language ("presents code relying on relatively high-level libraries before going into the lower-level details (necessary to build those libraries)"; "presents common and useful techniques and features before details").
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Jul 2013
    Posts
    576

    Re: remove duplicated from sorted vector

    Quote Originally Posted by laserlight View Post
    On the other hand, it may be good to learn how to use std::unique and then implement it, similiar to the learning method suggested by Stroustrup in his essay on Learning Standard C++ as a New Language
    Sure, everyone may benefit from the component based approach to programming. Here's a series of video lectures from the master himself, the author of the STL library, Mr. Stepanov,

    https://www.youtube.com/playlist?lis...ahwdtpZsmo4BGD

    But at this level of course it's no longer a question of using a standard function once in a while. It's a way of programming.

    And while I'm at it. With C++ 11 and 14 also seasoned programmers become newbies. Just watch this talk by Herb Sutter at CppCon 2014,

    https://www.youtube.com/watch?v=xnqTKD8uD64

    What? Putting all type information to the right!
    Last edited by razzle; December 3rd, 2014 at 01:52 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured