如何计算字符串到int的映射之间的所有排列

How to compute all permutations between a map of strings to ints

本文关键字:映射 之间 排列 int 何计算 计算 字符串      更新时间:2023-10-16

基本上,我想创建字符串向量到int向量的所有可能映射。我有以下内容:

std::vector<std::string> my_strings;
std::vector<unsigned int> my_ints;
my_strings.size() = 20; // not code, just for demonstration.
my_ints.size() = 4; // not code, just for demonstration.
std::vector<std::map<std::string, unsigned int> > all_possible_joint_mappings;

因此,我想用my_stringsmy_ints的所有可能的排列来填充all_possible_joint_mappings。实现这一点的好方法是什么?

联合映射的一个例子是:

string_1 -> int_1
string_2 -> int_1
string_3 -> int_1
string_4 -> int_4
string_5 -> int_2
...
string_20 -> int_3

您只需迭代一个集合,然后在内部循环中迭代另一个集合:

std::vector<std::pair<std::string, unsigned int> > all_possible_joint_mappings;
for ( std::vector<std::string>::const_iterator s = my_strings.begin(); s != my_strings.end(); ++s )
{
    for ( std::vector<unsigned int>::const_iterator i = my_ints.begin(); i != my_ints.end(); ++i )
        all_possible_joint_mappings.push_back( std::make_pair( *s, *i ) );
    }
}

注意,向量只需要包含对就可以完成这项工作。

亲切问候Torsten

如果希望从每个string值映射到多个int值,并且希望所有排列都在一个容器中,则使用专门为此目的设计的容器类型(std::multimap)。这里有一个例子:

#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <iostream>
int main()
{
   std::vector<std::string> strings;
   std::vector<int> ints;
   strings.push_back("One");
   strings.push_back("Two");
   strings.push_back("Three");
   strings.push_back("Four");
   strings.push_back("Five");
   ints.push_back(1);
   ints.push_back(2);
   ints.push_back(3);
   typedef std::multimap<std::string, int> SIMAP;
   SIMAP string_to_ints;
   std::for_each(
      strings.cbegin(),
      strings.cend(),
      [&ints, &string_to_ints] (const std::string& s)
   {
      std::for_each(
         ints.cbegin(),
         ints.cend(),
         [&] (const int i)
      {
         string_to_ints.insert(std::make_pair(s,i));
      });
   });
   std::for_each(
      string_to_ints.cbegin(),
      string_to_ints.cend(),
      [] (const SIMAP::value_type& mapping)
   {
      std::cout
         << mapping.first << " -> " << mapping.second << "n";
   });
   return 0;
}

下面的代码将递归地生成所有3个字母的单词的std::vector< std::vector<int> >(按字典顺序),其中每个字母来自一个4个字母的字母表。有64 = 4^3这样的词。

注意,一个简单的双循环是不够的,你需要在单词中的每个字母上递归,每个字母都需要一个循环。对于K字母表中的N字母单词,总复杂度为O(K^N),而不是像双环那样的O(K*N)

它以一种直接的方式从一个4个字母的字母表中概括出20个字母的单词(尽管这是2^40=1e12个不同的单词)。当然,将它们与原始字符串相匹配是一种简单的方式。

#include <array>
#include <cstddef>
#include <vector>
#include <iostream>
template<typename T, int K, int N>
void generate_all_multisets(
    std::array<T, K> const& alphabet, 
    std::vector< std::vector<T> >& all_words, 
    std::vector<T>& current_word, 
    int current_letter
)
{
    if (current_letter == N) {
        all_words.push_back(current_word);
        for (auto k = 0; k != N; ++k) 
            std::cout << current_word[k];
        std::cout << "n";
        return;
    }   
    auto const tmp = current_word[current_letter];
    for (auto letter = alphabet.begin(); letter != alphabet.end(); ++letter) {
        current_word[current_letter] = *letter;
        generate_all_multisets<T, K, N>(alphabet, all_words, current_word, current_letter + 1);
    }
    current_word[current_letter] = tmp; 
}
template<typename T, int K, int N>
void generate_all_words(
    std::array<T, K> const& alphabet, 
    std::vector< std::vector<T> >& all_words
)
{
    // first word
    std::vector<T> word(N, alphabet.front());
    generate_all_multisets<T, K, N>(alphabet, all_words, word, 0);
}
int main()
{
    std::array<int, 4> alphabet = { 1, 2, 3, 4};
    auto const word_length = 3; 
    std::vector< std::vector<int> > all_words;
    generate_all_words<int, 4, 3>(alphabet, all_words);
    return 0;
}

编辑:Ideone 上的输出