我不知道该搜索什么.我发现重命名地图迭代器的第一个和第二个,但它不是我想要做的.
这是我想要做的[见下面的无意义的C++代码].有可能接近这个吗?否则将只需要选择"调整"迭代器作为循环内部的第一行我想.
// what I want to do:
std::map<int, std::string> my_map;
// ... populate my_map
for(auto key, auto & value: my_map){
// do something with integer key and string value
}
Run Code Online (Sandbox Code Playgroud)
C++ 11很好,但如果可能,请避免使用.
我得到的最接近的是
// TODO, can this be templated?
struct KeyVal{
int & id;
std::string & info;
template <typename P>
KeyVal(P & p)
: id(p.first)
, info(p.second)
{
}
};
//...
for ( KeyVal kv : my_map ){
std::cout << kv.info;
}
Run Code Online (Sandbox Code Playgroud)
但这意味着为每个地图编写一个适配器类:(
// slightly joke answer/"what …
Run Code Online (Sandbox Code Playgroud) 是否可以在dict文字中使用"可选"键,而不是将它们添加到if
语句中?
像这样:
a = True
b = False
c = True
d = False
obj = {
"do_a": "args for a" if a,
"do_b": "args for b" if b,
"do_c": "args for c" if c,
"do_d": "args for d" if d,
}
#expect:
obj == {
"do_a": "args for a",
"do_c": "args for c",
}
Run Code Online (Sandbox Code Playgroud)
编辑上下文:我知道如何做逻辑:)我只是想避免if
语句,因为我的对象是一个代表声明性逻辑的大块数据,所以移动的东西有点像"意大利面条编码"的东西不是根本就是程序性的.我希望对象的值"看起来像它意味着什么"作为查询.
它实际上是一个弹性搜索查询,所以它看起来像这样:
{
"query": {
"bool": {
"must": [
<FILTER A>,
<FILTER B>, # would like to make this filter optional …
Run Code Online (Sandbox Code Playgroud)