C++ 在C+中查找唯一字符串+;,并生成相关的查找向量

C++ 在C+中查找唯一字符串+;,并生成相关的查找向量,c++,string,vector,unique,C++,String,Vector,Unique,A在c++中有一个字符串向量: vector<string> myVect = {"A", "A", "A", "B", "B", "A", "C", "C", "foo", "A", "foo"}; 另外,我想要一个唯一字符串的向量,每个位置对应于out中的数字: uniqueStrings = {"A", "B", "C", "foo"} 到目前为止,我有以下几点: vector<string> uniqueStrings; // stores list

A在c++中有一个字符串向量:

vector<string> myVect = {"A", "A", "A", "B", "B", "A", "C", "C", "foo", "A", "foo"};
另外,我想要一个唯一字符串的向量,每个位置对应于
out
中的数字:

uniqueStrings = {"A", "B", "C", "foo"}
到目前为止,我有以下几点:

  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();
    }

  }
向量唯一字符串;//存储所有唯一字符串的列表
向量输出(myVect.size());
对于(int i=0;i

这是可行的,但肯定有更好的方法吗?

继续在一个映射中按它们,其中字符串是键,值对应于每个字符串的id。然后,映射的值将唯一地对应于字符串,键将是唯一的字符串。

继续在映射中按它们,其中字符串是键,值对应于每个字符串的id。然后,映射的值将唯一地对应于字符串,并且键将是唯一的字符串。

使用

#包括
...
设置唯一字符串;
...
对于(int i=0;i
使用一个

#包括
...
设置唯一字符串;
...
对于(int i=0;i
以下是一个或多或少完整的示例,说明如何使用
std::map
来维护唯一字符串到整数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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//一种简单的函子类型,可以更轻松地转储
//简单值的容器或std::pair的容器
结构转储
{
模板
void运算符()(typename std::pair const&x)
{

cout以下是一个或多或少完整的示例,说明如何使用
std::map
来维护唯一字符串到整数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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//一种简单的函子类型,可以更轻松地转储
//简单值的容器或std::pair的容器
结构转储
{
模板
void运算符()(typename std::pair const&x)
{

作为一个提示,你可以考虑看<代码> STD::MAP < /代码>或者类似于把一个字符串和一个数字联系起来的东西。不是家庭作业,我试图计算输入向量来输入机器学习算法。我习惯了MATLAB的简洁性,在这里,上面所有代码都是<代码> [UNQuiString,~,OU]。作为一个提示,你可以考虑看<代码> STD::map < /COD>或类似于把一个字符串与一个数字联系起来的东西。不是作业,我试图计算输入向量以输入机器学习算法。我习惯于MATLAB的简洁性,上面的代码全部是<代码>。[uniqueStrings,~,out]=唯一(myVect)
#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;
}