C++ 中的简洁列表/向量
我目前正在将 Python 中的算法翻译为 C++。
此行 EXCH_SYMBOL_SETS = [["i", "1", "l"], ["s", "5"], ["b", "8"], ["m", "n" ]]
现在
vector<vector<char>> exch_symbols;
vector<char> vector_1il;
vector_1il.push_back('1');
vector_1il.push_back('i');
vector_1il.push_back('l');
vector<char> vector_5s;
vector_5s.push_back('5');
vector_5s.push_back('s');
vector<char> vector_8b;
vector_8b.push_back('8');
vector_8b.push_back('b');
vector<char> vector_mn;
vector_mn.push_back('m');
vector_mn.push_back('n');
exch_symbols.push_back(vector_1il);
exch_symbols.push_back(vector_5s);
exch_symbols.push_back(vector_8b);
exch_symbols.push_back(vector_mn);
我讨厌为二维向量中的每个内部变量都有一个中间命名变量。我不太熟悉 C++ 中的多维数据结构。有更好的办法吗?
之后发生的事情是这样的:
multimap<char, char> exch_symbol_map;
/*# Insert all possibilities
for symbol_set in EXCH_SYMBOL_SETS:
for symbol in symbol_set:
for symbol2 in symbol_set:
if symbol != symbol2:
exch_symbol_map[symbol].add(symbol2)*/
void insert_all_exch_pairs(const vector<vector<char>>& exch_symbols) {
for (vector<vector<char>>::const_iterator symsets_it = exch_symbols.begin();
symsets_it != exch_symbols.end(); ++symsets_it) {
for (vector<char>::const_iterator sym1_it = symsets_it->begin();
sym1_it != symsets_it->end(); ++sym1_it) {
for (vector<char>::const_iterator sym2_it = symsets_it->begin();
sym2_it != symsets_it->end(); ++sym2_it) {
if (sym1_it != sym2_it) {
exch_symbol_map.insert(pair<char, char>(*sym1_it, *sym2_it));
}
}
}
}
}
所以这个算法应该以一种或另一种方式与这里的表示一起工作。目标是以后可以轻松更改 EXCH_SYMBOL_SETS
以包含新的 char
组或向现有组添加新字母。谢谢你!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会重构,而不是
vector
,使用std::string
作为内部,即然后更改您的插入方法:
I would refactor, instead of
vector<char>
, usestd::string
as internal, i.e.then change your insert method:
您可以通过删除中间值来缩短它
您也可以使用 boost.assign 或类似的东西
EXCH_SYMBOL_SETS = [["i", "1", "l"], ["s", "5"], ["b", "8"], ["m", "n"]]
然后变成向量<向量<字符>> exch_symbols(list_of(向量(list_of('i')('1')('l')))(向量(list_of('s')('5'))(list_of('m') ')('n')))
(未经测试,也从未将其与嵌套向量一起使用,但它应该是这样的)You could shorten it by getting rid of the intermediate values
You could also use boost.assign or something similiar
EXCH_SYMBOL_SETS = [["i", "1", "l"], ["s", "5"], ["b", "8"], ["m", "n"]]
then becomesvector<vector<char>> exch_symbols(list_of(vector<char>(list_of('i')('1')('l')))(vector<char>(list_of('s')('5'))(list_of('m')('n')))
(not tested and never used it with nested vectors, but it should be something like this)对于你真正的问题...
没有直接转换 - 您已从存储相同类型的值切换为存储变量类型的值。 Python 允许这样做是因为它是一种动态类型语言,而不是因为它具有更好的数组语法。
有多种方法可以复制 C++ 中的行为(例如 boost::any 或 boost::variant 的向量,或支持此行为的用户定义的容器类),但它永远不会像 Python 中那样简单。
For your real question of...
There is no direct translation - you've switched from storing values of the same type to storing values of variable type. Python allows this because it's a dynamically typed language, not because it has a nicer array syntax.
There are ways to replicate the behaviour in C++ (e.g. a vector of boost::any or boost::variant, or a user defined container class that supports this behviour), but it's never going to be as easy as it is in Python.
您的代码:
简洁代码:
您觉得可以吗?
如果您想按照 std::string >Nim,那么你甚至可以使用这个:
休息你可以按照尼姆的帖子。 :-)
Your code:
Concise code:
Is it fine with you?
If you want to use
std::string
as suggested by Nim, then you can use even this:Rest you can follow Nim's post. :-)
在c++0x中指令
向量<字符串> EXCH_SYMBOL_SETS={"i1l", "s5", "b8", "mn"} ;
编译并运行良好。可悲的是,明显相似的声明
向量<向量<字符>> EXCH_SYMBOL_SETS={{'i','1','l'},{'s','5'},{'b','8'},{'m','n'}};
不起作用:-(。
这是在 g++ 4.5.0 或更高版本中实现的,您应该添加
-std=c++0x
选项。我认为此功能在 microsoft c ( VC10),不知道其他编译器的状况如何。In c++0x the instruction
vector<string> EXCH_SYMBOL_SETS={"i1l", "s5", "b8", "mn"} ;
compiles and works fine. Sadly enough the apparently similar statement
vector<vector<char>> EXCH_SYMBOL_SETS={{'i','1','l'},{'s','5'}, {'b','8'}, {'m','n'}};
doesn't work :-(.
This is implemented in g++ 4.5.0 or later you should add the
-std=c++0x
option. I think this feature is not yet avaliable in microsoft c (VC10), and I don't know what's the status of other compilers.我知道这是一篇旧文章,但以防万一有人偶然发现它,
C++
在处理这些东西方面已经MUCH 做得更好了:在
c++11 中
第一个代码块可以简单地重写为:这对于
string
来说也不是特殊的,我们可以像这样嵌套vector
:整个代码采用
c++14
风格,并在末尾添加了cout
:I know that this is an old post, but in case anyone stumbles across it,
C++
has gotten MUCH better at dealing with this stuff:In
c++11
the first code block can simply be re-written in as:This isn't special to
string
either, we can nestvector
like so:And here's the entire code in
c++14
-style, with an addedcout
at the end: