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 7490129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:33:17+00:00 2026-05-29T15:33:17+00:00

A have a vector of strings in c++: vector<string> myVect = {A, A, A,

  • 0

A have a vector of strings in c++:

vector<string> myVect = {"A", "A", "A", "B", "B", "A", "C", "C", "foo", "A", "foo"};

How can I convert this to a vector of integers, so that each integer uniquely corresponds to a string in myVect?
i.e. I would like a vector

out = {0, 0, 0, 1, 1, 0, 2, 2, 3, 0, 3}

In addition, I would like a vector of the unique strings, each position corresponding to the number in out:

uniqueStrings = {"A", "B", "C", "foo"}

So far I have the following:

  vector<string> uniqueStrings;   // stores list of all unique strings
  vector<int> out(myVect.size());

  for (int i = 0; i < myVect.size(); ++i)
  {

    // seeing if this string has been encountered before
    bool assigned = false;
    for (int j = 0; j < uniqueStrings.size(); ++j)
      if (!myVect.at(i).compare( uniqueStrings.at(j) ))
      {
        out.at(i) = j;
        assigned = true;
        break;
      }

    // if not, add new example to uniqueStrings
    if (!assigned)
    {
      uniqueStrings.push_back(myVect.at(i));
      out.at(i) = uniqueStrings.size();
    }

  }

This works, but surely there must be a better way?

  • 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-29T15:33:17+00:00Added an answer on May 29, 2026 at 3:33 pm

    Here’s a more or less complete example of how you might use a std::map<> to maintain a mapping of unique strings to an integer ID:

    #include <algorithm>
    #include <iostream>
    #include <map>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    
    // a simple functor type that makes it easier to dump the contents of a 
    //  container of simple values or a container of std::pair
    struct dump
    {
        template <typename K, typename V>
        void operator()( typename std::pair<K,V> const& x)
        {
            cout << x.first << " ==> " << x.second << endl;
        }
    
        template <typename T>
        void operator()( T const& x)
        {
            cout << x << endl;
        }
    };
    
    
    
    #define NUM_ELEM(x) (sizeof(x)/sizeof(x[0]))
    
    char const* data[] = {"A", "A", "A", "B", "B", "A", "C", "C", "foo", "A", "foo"};
    
    int main() {
        // intialize the data set
        vector<string> myVect( data, data + NUM_ELEM(data));
    
        cout << "dump of initial data set" << endl << endl;
        for_each( myVect.begin(), myVect.end(), dump());
    
        map<string,size_t> uniqueStrings;   // stores collection of all unique strings
    
        for (vector<string>::iterator i = myVect.begin(); i != myVect.end(); ++i) {
            // I'm using uniqueStrings.size() as a convenience here...
            // I just needed something to generate  unique ID's easily,
            // it might not be appropriate to use size() for your ID's in real life
    
            // this will insert the new mapping if there's not already one 
            uniqueStrings.insert( make_pair(*i, uniqueStrings.size()));
        }
    
    
        cout << endl << endl<< "dump of uniqueStrings" << endl << endl;
        for_each( uniqueStrings.begin(), uniqueStrings.end(), dump());
    
        // I'm not sure if you'd need this `out` vector anymore - you can probably just
        //  use the `uniqueStrings` map directly for this information (but that would
        //  depend on your specific needs)
    
        vector<int> out;
        for (vector<string>::iterator i = myVect.begin(); i != myVect.end(); ++i) {
            out.push_back( uniqueStrings[*i]);
        }
    
        cout << endl << endl << "dump of `out` vector" << endl << endl;
        for_each( out.begin(), out.end(), dump());
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a std::vector<std::string> m_vPaths; I iterate over this vector and call ::DeleteFile(strPath) as
I have a std::vector<uint8_t> that contains strings at specific offsets. Here's a shortened dump:
I have a vector that I want to insert into a set . This
I have a function where I have a container which holds strings (eg vector<string>
I have a large collection of unique strings (about 500k). Each string is associated
I have a Vector < String >. Now i want to store those Strings
I have a vector of strings. Need help figuring out how to convert it
I have vector of strings and want to create a fixed with string out
I have a vector of strings: std::vector<std::string> fName which holds a list of file
I have made a string vector vector<string> actor_; and then added elements in it

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.