Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 关于map::erase和map::count_C++_String_Dictionary - Fatal编程技术网

C++ 关于map::erase和map::count

C++ 关于map::erase和map::count,c++,string,dictionary,C++,String,Dictionary,关于map::count()和map::erase()的使用,我有一个简单的问题。我将它们与std::string一起使用,但想知道是否需要使用string::c_str()。例如,“我的代码”当前显示为: void Person::removeFriend(std::string first, std::string last){ std::string name = (first + last); //checks to ensure the friend exists in

关于map::count()和map::erase()的使用,我有一个简单的问题。我将它们与std::string一起使用,但想知道是否需要使用string::c_str()。例如,“我的代码”当前显示为:

void Person::removeFriend(std::string first, std::string last){
    std::string name = (first + last);
    //checks to ensure the friend exists in the user's friend list
    if (_friends.count(name) == 1){
        _friends.erase(name);
    }
}
那么我的问题是,它是否真的是这样的:

void Person::removeFriend(std::string first, std::string last){
    std::string name = (first + last);
    //checks to ensure the friend exists in the user's friend list
    if (_friends.count(name.c_str()) == 1){
        _friends.erase(name.c_str());
    }
}

另外,我想这也适用于map::insert()。我只从使用std::string打开文件中知道这个用法。任何和所有的建议都是非常感谢提前

不,没有理由在这里使用
c_str
。在使用
擦除之前,不需要检查是否存在。您的功能可以简单如下:

void Person::removeFriend(const std::string& first, const std::string& last){
    std::string name = (first + last);
    _friends.erase(name);
}
。。。甚至:

void Person::removeFriend(const std::string& first, const std::string& last){
    _friends.erase(first + last);
}

不,没有理由在这里使用
c_str
。在使用
擦除之前,不需要检查是否存在。您的功能可以简单如下:

void Person::removeFriend(const std::string& first, const std::string& last){
    std::string name = (first + last);
    _friends.erase(name);
}
。。。甚至:

void Person::removeFriend(const std::string& first, const std::string& last){
    _friends.erase(first + last);
}

std::map
有一个只需按键的重载

size_type erase( const key_type& key );
您可以简单地使用:

std::string name = (first + last);
_friends.erase(name);

std::map
有一个只需按键的重载

size_type erase( const key_type& key );
您可以简单地使用:

std::string name = (first + last);
_friends.erase(name);

文件流需要
c_str
的唯一原因是,它们过去只接受
const char*
,如果给定
std::string
,就不会编译。假设您的
std::map
键首先定义为
std::string
,那么可以将
std::string
值传递给
count()
擦除()。顺便说一句,使用
count()
并不是测试是否存在的最佳方法,您可以使用
find()
。它向第一个匹配键返回一个
迭代器
,然后您可以使用该迭代器
erase()
该特定项。如果按键值执行
erase()
,则必须再次搜索该键值,这是您事先已找到的,因此第二次搜索是多余的,可能会浪费时间,具体取决于地图中的项目数。我不确定您为什么要费心于
计数
查找
。如果你想删除它,只要
删除
。Fred的意思是如果你按键值
删除()
,它会删除找到的项,否则它不会删除任何内容,也不会抛出错误。该版本的
erase()
返回实际删除的项目数。因此,
count(name)
iter=find(name);擦除(iter)erase(name)
时,code>和
都是多余的。@CreasyBear,仅供参考,所有标准库通常都有。文件流需要
c_str
的唯一原因是,它们过去只接受
const char*
,如果给定
std::string
,则不会编译。假设您的
std::map
键首先定义为
std::string
,则可以将
std::string
值传递给
count()
erase()
。顺便说一句,使用
count()
并不是测试是否存在的最佳方法,您可以使用
find()
。它向第一个匹配键返回一个
迭代器
,然后您可以使用该迭代器
erase()
该特定项。如果按键值执行
erase()
,则必须再次搜索该键值,这是您事先已找到的,因此第二次搜索是多余的,可能会浪费时间,具体取决于地图中的项目数。我不确定您为什么要费心于
计数
查找
。如果你想删除它,只要
删除
。Fred的意思是如果你按键值
删除()
,它会删除找到的项,否则它不会删除任何内容,也不会抛出错误。该版本的
erase()
返回实际删除的项目数。因此,
count(name)
iter=find(name);擦除(iter)erase(name)
本身时,code>都是多余的。@CreasyBear,仅供参考,通常所有标准库都有。