一、mapfind函数
mapfind函数是C++ STL中关联式容器map中的一个成员函数,用于在map中查找键值对应的value值。
//示例代码,使用mapfind函数查找key对应的value
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string,int> mymap;
mymap["apple"] = 1;
mymap["banana"] = 2;
mymap["orange"] = 3;
string key = "apple";
int value = mymap.mapfind(key);
cout << key << " " << value << endl;
return 0;
}
在上面的代码中,我们创建了一个map容器,将字符串”apple”作为键,将整数1作为值存入map中。然后,我们使用mapfind函数查找”apple”对应的值,并将其输出。
二、mapfind函数返回
如果mapfind函数在map中找到了给定的键值,它将返回对应的值。如果没有找到,mapfind函数将返回一个默认值,这个默认值与map容器中存储的value类型有关。如int类型的默认值为0,string类型的默认值为空字符串。
//示例代码,返回默认值
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string,int> mymap;
mymap["apple"] = 1;
mymap["banana"] = 2;
mymap["orange"] = 3;
string key = "watermelon";
int value = mymap.mapfind(key);
cout << key << " " << value << endl;
return 0;
}
在上面的代码中,我们使用了一个在map中不存在的键”watermelon”,mapfind函数将返回默认值0,并将其输出。
三、mapfind找不到返回什么
mapfind函数在找不到对应的键值时,将返回一个默认值。对于基本类型的值,如int、double等,默认值为0。对于类类型的值,将默认构造一个该类型的对象。
//示例代码,返回默认构造的对象
#include <iostream>
#include <map>
#include <string>
using namespace std;
class Person{
public:
string name;
int age;
};
int main(){
map<string,Person> mymap;
mymap["Tom"].name = "Tom";
mymap["Tom"].age = 18;
string key = "Jerry";
Person value = mymap.mapfind(key);
cout << key << " " << value.name << " " << value.age << endl;
return 0;
}
上面这段代码中,我们在map容器中创建了一个Person对象,并将其存入map中。然后,我们使用一个不存在于map中的键”Jerry”调用mapfind函数,mapfind函数将返回默认构造的Person对象,并将其输出。
四、find的用法
与mapfind函数不同,find函数不是map容器的成员函数,而是在头文件中提供的函数,用于在STL容器中查找元素。find函数的返回值是一个迭代器,指向容器中第一个等于待查找元素的元素。如果找不到,则返回容器的end迭代器。
//示例代码,使用find函数查找key对应的value
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string,int> mymap;
mymap["apple"] = 1;
mymap["banana"] = 2;
mymap["orange"] = 3;
string key = "apple";
auto it = mymap.find(key);
if(it != mymap.end()){
int value = it->second;
cout << key << " " << value << endl;
}
return 0;
}
在上面的代码中,我们使用了find函数查找键为”apple”的键值对,并将其输出。如果找到了,我们使用迭代器访问对应的value值。否则,我们不执行任何操作。
五、find out
在使用STL容器的时候,我们常常需要查找容器中的元素。除了find函数,STL中还有一些其他的查找函数可供使用,如count、lower_bound、upper_bound等。
count函数用于在容器中查找某个值的出现次数。如果该值出现了n次,count函数将返回n。
//示例代码,使用count函数查找值的出现次数
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string,int> mymap;
mymap["apple"] = 1;
mymap["banana"] = 2;
mymap["orange"] = 3;
int count = mymap.count("apple");
cout << "apple count: " << count << endl;
return 0;
}
在上面的代码中,我们使用count函数查找”apple”出现的次数,并将次数输出。
lower_bound和upper_bound函数用于在有序容器中查找某个值。lower_bound函数返回第一个大于或等于给定值的迭代器,upper_bound函数返回第一个大于给定值的迭代器。
//示例代码,使用lower_bound和upper_bound函数查找值在有序容器中的位置
#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> myset;
myset.insert(1);
myset.insert(3);
myset.insert(5);
auto low = myset.lower_bound(2);
auto up = myset.upper_bound(4);
cout << "lower bound: " << *low << endl;
cout << "upper bound: " << *up << endl;
return 0;
}
在上面的代码中,我们使用lower_bound函数查找第一个大于或等于2的元素,并使用upper_bound函数查找第一个大于4的元素。由于set容器是有序的,所以它们的返回值分别为3和5。
六、map find和count 性能
在使用STL容器时,我们需要考虑它们的性能。在map容器中,find函数和count函数是用于查找元素的常用函数。find函数的时间复杂度为O(logN),count函数的时间复杂度为O(logN)。在实际使用时,我们应该根据具体的情况选择使用哪一个函数。
//示例代码,比较map find和count函数的性能
#include <iostream>
#include <map>
#include <chrono>
using namespace std;
int main(){
map<int,int> mymap;
for(int i=0; i<1000000; i++){
mymap[i] = i+1;
}
auto start = chrono::high_resolution_clock::now();
int count = mymap.count(999999);
auto end = chrono::high_resolution_clock::now();
cout << "count time: " << chrono::duration_cast<chrono::microseconds>(end-start).count() << endl;
start = chrono::high_resolution_clock::now();
auto it = mymap.find(999999);
int value = it->second;
end = chrono::high_resolution_clock::now();
cout << "find time: " << chrono::duration_cast<chrono::microseconds>(end-start).count() << endl;
return 0;
}
在上面的代码中,我们创建了一个包含1000000个元素的map容器。然后,我们使用count函数和find函数分别查找键值为999999的元素,并比较它们的时间复杂度。
根据实验结果,count函数的执行时间约为400微秒,find函数的执行时间约为200微秒。因此,在多次查找同一个元素的情况下,使用find函数的效率更高。
原创文章,作者:RQRNQ,如若转载,请注明出处:https://www.506064.com/n/331899.html