如何计算字符串到int的映射之间的所有排列
How to compute all permutations between a map of strings to ints
基本上,我想创建字符串向量到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_strings
到my_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 上的输出
相关文章:
- 常量映射和常量元素映射之间的区别
- 如何在没有映射的情况下在枚举和字符串之间进行转换?
- C++11 在列表到映射(或其他容器)之间移动元素
- 在类之间传递映射
- 两个进程之间的共享映射内存在编辑时未更新
- 在 C++ 和 C# 之间共享内存映射文件结构
- 遍历std ::映射,效果少于迭代器之间的比较
- 本机C++类与 WinRT 组件(引用类)之间的自动映射
- 映射::擦除:按键擦除或迭代器擦除之间的区别
- 消除类似映射和类似矢量的容器之间的模板专用化
- 高效实现 64 位和 32 位无符号整数之间的双向映射
- 两个映射之间的分配-移动语义和性能
- 如何计算字符串到int的映射之间的所有排列
- Boost管理的映射文件:在OSX和Linux之间不兼容
- 将 1 到 12 之间的数字映射到月份名称
- 希望有效地克服 Boost.Process 间共享内存中映射中键类型之间的不匹配
- 映射和unorderd_map之间的 C 实现差异
- 计算表示稀疏向量 c++ 的映射之间的距离
- c++ 11 STL在两个映射之间使用move赋值合并
- 在 SWIG 传入和 freearg 类型映射之间传递信息