Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:29:28+00:00 2026-05-31T08:29:28+00:00

I have a std::vector and I want to check all the elements in it.

  • 0

I have a std::vector and I want to check all the elements in it. If a certain element appears more than once, I signal an error.

This is how I did it:

std::vector<std::string> test;
test.push_back("YES");
test.push_back("YES");

for(int i = 0; i < test.size(); i++)
{
    if(test[i] > 1)
    {
        DCS_LOG_DEBUG("ERROR WITH COUNT")
    }
}

This did not work though I know how to count using the std::vector::count() method. But I want to get the count for each element, as opposed to counting everything… any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:29:29+00:00Added an answer on May 31, 2026 at 8:29 am

    The simplest way is to std::sort the vector and then use std::adjacent_find.


    However, if you don’t want to sort the vector, you can do something like this in C++11:

    #include <unordered_map>
    #include <functional> // For std::hash<std::string>.
    #include <string>
    #include <iostream>
    
    int main() {
    
        // Test data.
        std::vector<std::string> v;
        v.push_back("a");
        v.push_back("b");
        v.push_back("c");
        v.push_back("a");
        v.push_back("c");
        v.push_back("d");
        v.push_back("a");
    
        // Hash function for the hashtable.
        auto h = [](const std::string* s) {
            return std::hash<std::string>()(*s);
        };
    
        // Equality comparer for the hashtable.
        auto eq = [](const std::string* s1, const std::string* s2) {
            return s1->compare(*s2) == 0;
        };
    
        // The hashtable:
        //      Key: Pointer to element of 'v'.
        //      Value: Occurrence count.
        std::unordered_map<const std::string*, size_t, decltype(h), decltype(eq)> m(v.size(), h, eq);
    
        // Count occurances.
        for (auto v_i = v.cbegin(); v_i != v.cend(); ++v_i)
            ++m[&(*v_i)];
    
        // Print strings that occur more than once:
        for (auto m_i = m.begin(); m_i != m.end(); ++m_i)
            if (m_i->second > 1)
                std::cout << *m_i->first << ": " << m_i->second << std::endl;
    
        return 0;
    
    }
    

    This prints:

    a: 3
    c: 2
    

    I didn’t actually benchmark it, but this has a chance for being rather performant, for following reasons:

    • Assuming the actual vector elements do not produce pathologically lopsided hashes, this is actually an O(n) algorithm, as opposed to O(n*log(n)) for sorting.
    • We are using the hashtable of pointers to strings, not strings themselves, so there is no unnecessary copying taking place.
    • We can “pre-allocate” hashtable buckets (we pass v.size() when constructing m), so hashtable resizes are minimized.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several std::vector , all of the same length. I want to sort
I have a std::vector<int> , and I want to delete the n th element.
I have four std::vector containers that all might (or might not) contain elements. I
I have a std::vector and I want to check a specific attribute of each
Possible Duplicate: sum of elements in a std::vector I have std::vector<int> and I want
I have a std::vector. I want to create iterators representing a slice of that
If I have a std::vector or std::map variable, and I want to see the
I have some numbers stored in a std::vector<int> . I want to find which
I currently have a std::vector which holds std::vector of double. I'd want to sort
Let's say I have a class FOO. I want to have a std::vector of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.