相关疑难解决方法(0)

如何查找C++ std :: map中是否存在给定键

我正在尝试检查给定的密钥是否在地图中,有些不能这样做:

typedef map<string,string>::iterator mi;
map<string, string> m;
m.insert(make_pair("f","++--"));
pair<mi,mi> p = m.equal_range("f");//I'm not sure if equal_range does what I want
cout << p.first;//I'm getting error here
Run Code Online (Sandbox Code Playgroud)

那么如何打印p中的内容呢?

c++ dictionary stl

412
推荐指数
9
解决办法
50万
查看次数

什么是Python的"in"运算符的C++等价物?

检查元素是否包含在数组/列表中的C++方法是什么,类似于inPython中的运算符?

if x in arr:
    print "found"
else
    print "not found"
Run Code Online (Sandbox Code Playgroud)

与Python的in运算符相比,C++等价物的时间复杂度如何?

c++ arrays

46
推荐指数
5
解决办法
1万
查看次数

如何查看地图中是否存在密钥?

我有两个stl地图map<int,int> ,我想比较它们..所以这里是代码..

map <int, int> a,b;
insert into a and b;
map<int,int>::iterator i;

for(i=a.begin();i!=a.end();i++){
    if(what should be here?)
         then cout << (*i).first << " also present in b" << endl;
}
Run Code Online (Sandbox Code Playgroud)

我希望像(b [(*i).first])之类的东西存在?

c++ stl map

2
推荐指数
1
解决办法
1379
查看次数

find()是否也返回C++中地图的最后一个元素?

我想使用find()方法来捕获我的std :: map()的特定元素

现在返回值是我正在寻找的元素,或者它指向地图的末尾(如果找不到元素)(Cf:http://www.cplusplus.com/reference/map/map/找/).

所以我正在使用这两个信息来决定元素是否在地图中.但是如果元素已经在内部但在最后呢?会发生什么?然后,我的if-query将决定它不存在,因此我的程序将被编制.

我是否正确使用这个例程,如果,如何预防呢?

感谢您的支持

c++ map find

2
推荐指数
1
解决办法
174
查看次数

标签 统计

c++ ×4

map ×2

stl ×2

arrays ×1

dictionary ×1

find ×1