liunx c++ stdmapstdstring int map_absFilena判断判断插入是否成功 使用c++98标准实现给出实现
在C++98标准中,可以使用std::pair的返回值来判断插入是否成功。
#include <iostream>
#include <map>
#include <string>
int main()
{
std::map<std::string, int> map_absFilena;
// 插入元素并判断是否插入成功
std::pair<std::map<std::string, int>::iterator, bool> result = map_absFilena.insert(std::make_pair("filename", 10));
if (result.second)
{
std::cout << "Insertion successful" << std::endl;
}
else
{
std::cout << "Insertion failed" << std::endl;
}
return 0;
}
在上述代码中,使用std::map的insert函数插入一个键值对("filename", 10),并将返回值赋给result。result.second表示插入是否成功,如果为true则表示插入成功,如果为false则表示插入失败。
请注意,C++98标准不支持直接使用auto推导返回值类型,因此我们需要显式地指定std::pair<std::map<std::string, int>::iterator, bool>作为返回值类型。
原文地址: https://www.cveoy.top/t/topic/jcSS 著作权归作者所有。请勿转载和采集!