CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2015
    Posts
    500

    find if any duplicates in a vector of structs based on an attribute

    Hello,

    I have a vector of structs (or classes):
    std::vector<ConfigData>& vecConfigData;

    Code:
    class ConfigData
    {
    public:
    
    
    protected:
    	   CString m_strRegionName;
    	int		m_nId = 0;
    :
    :
    :
    };
    This is already existing . I want to add a warning message, if there are any m_nId are duplicates.

    If it is just vector, it is easier, i guess i can use the std::adjacent_find. But in this case, is there anything i can use like this or do i just need to loop through all and find duplicates? It will be very helpful, if you can help me with better way of doing this.

    thanks a lot
    pdk
    Last edited by pdk5; January 18th, 2021 at 07:35 AM.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: find if any duplicates within the struct in a vector

    You mention adding a warning message - so you want to insert the item even if m_nId is a duplicate?

    Is the vector sorted by m_nID?

    Unless the vector is sorted by m_nID, then adjacent_find() is no good - as it only checks for duplicate adjacent entries, which won't be true if the vector isn't sorted.

    If you have to use an unsorted vector, then find_if() is probably the best. For a sorted vector, then consider binary_search().

    However, IMO it would be preferable to use a sorted container by m_nID such as a map. If you can't and must retain the existing vector, then consider having a separate container of set to hold the unique values of m_nId that would be simple and quick to check for duplicate et al.
    Last edited by 2kaud; January 18th, 2021 at 10:21 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: find if any duplicates within the struct in a vector

    Thanks a lot kaud.

    The vector is not sorted.
    I donot want to insert any values.

    But i want to kind of send a message to user that something is wrong in his entry. (It is assumed user is not entering duplicates). But this is to validate his input into the struct of vectors.

    But as it is not just vector (but is struct of vectors), i am not sure how i can use the std:unique on the vector of structs..

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: find if any duplicates within the struct in a vector

    std::unique() removes duplicate entries - which is not what you want as duplicates are allowed. You need std::find_if() with the predicate lambda to check m_nId == <required value> this will iterate the entire vector until a match is found to see the entry exists.

    Or, as I mentioned, maintain a separate std::set of m_nId values for testing - with insertion/deletion done in step with the vector insertion/deletion.

    What is the max number of items that this vector might contain - and how frequently would you want to check for existing?

    Something like (not tried):

    Code:
    bool got_dup(const std::vector<ConfigData>& vec, int id)
    {
    	const auto fit = std::find_if(vec.begin(), vec.end(), [id](const auto& cd) {return cd.m_nId == id; });
    
    	return fit != vec.end();
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    May 2015
    Posts
    500

    Re: find if any duplicates within the struct in a vector

    Thanks a lot kaud.

    Actually, i want to check, if the m_nId is unique and no duplicates.

    As this is not very big vector (may have atmost 10 elements) and the check is not done many times, i think i can afford to make a copy of the vector and sort it, as you suggested.

    But I also need to check another member is also not duplicate., like lets say m_nId2. In this case, i endup doing two copy operations to check the m_nId2 there are no duplicates

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: find if any duplicates within the struct in a vector

    As this is not very big vector (may have atmost 10 elements) and the check is not done many times
    Then just do it as per the suggested function in post #4

    For m_nId2, just extend the condition check in the function.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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