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

  • Home
  • SEARCH
  • 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 7860185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:11:15+00:00 2026-06-02T22:11:15+00:00

I have the following data: FolioA Name1 100 FolioA Name2 110 FolioA Name3 100

  • 0

I have the following data:

FolioA Name1 100
FolioA Name2 110
FolioA Name3 100
FolioB Name1 100
FolioB Name3 106
FolioC Name1 108
FolioC Name2 102
FolioC Name3 110

I want to only insert unique names(i.e. Name1, Name2 and Name3, each once) into

std::vector<std::string> name;

as I iterate through the data.

So, I have the following code where I have stored the data in a map called test:

std::map<std::string, std::map<std::string, double> >test;
std::map<std::string, std::map<std::string, double > >::iterator it1 = test.begin(), end1 = test.end();
    while (it1 !=end1) {
        std::map<std::string, double>::iterator it2 = it1->second.begin(), end2=it1->second.end();
        **name.push_back(it2->first);**
        ++it2;
    }
    ++it1;
}

But, currently by pushing the data into name the way I am has 3 instances of Name1, 2 of Name2, and 3 of Name3, which is expected from my code. How do I fix it to only have unique names.

  • 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-06-02T22:11:16+00:00Added an answer on June 2, 2026 at 10:11 pm

    Since you want to keep the first instance for a given name, you will have to perform a name lookup at some point. A simple algorithm involving only your vector would be to can check if the the entry already exists using std::find

    std::vector<std::string> name;
    
    ....
    if (std::find(name.begin(), name.end(), someName) == name.end()) {
      // someName not in name, add it
      name.push_back(someName);
    }
    

    But here you are performing a search each time you want to insert an element, and this (by itself) is up to O(N) complexity, giving O(N*N) for the whole algorithm. So you could optimize by using an intermediary container with fast look up, such as an std::set as suggested by @Chad and which has O(logN) complexity for look-up, giving O(N*logN) overall, or a hash container such as C++11’s std::unordered_set, which has close to constant time look-up, giving ~O(N) overall complexity.

    #include <unordered_set>
    
    std::unordered_set<std::string> name_set;
    ....
    
    // still need to search, since you want to keep 
    // the first instance of each name, and not the last.
    // But unordered_set performs the look-up at insertion,
    // only inserting if someName not already in the set
    name_set.insert(someName);
    

    and then, following @Chad’s example,

    std::vector<std::string> name(names_set.begin(), name_set.end());
    

    If you don’t have C++11, hash map alternatives are boost::hash_map and tr1::hash_map.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have following json data which I want to pass it to server using
I have following data matrix, I want to iterate over this matrix and look
I have following data fields: int iData1 = 100; int iData2 = 5000; float
I have following data: Dictionary<string,string> dctParameters = new Dictionary(){ {a,var1},{b,var2},{c,var3},.... } I want to
I have following data frames > head(elo) date elo 1 1921-12-18 1597 2 1922-05-14
I have following data structure (simplified): A giant list of the class TestClass public
I have following data to save in database using php my data is :
I have following data in excel table r1 r2 r3 r4 r5 v1 v2
I have following data in a file called binFile 78 1 79 4 80

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.