与std :: string一起使用的std :: replace_copy的奇怪行为

 btsk@163.com 发布于 2023-02-08 20:26

我有这个简单的代码:

#include 
#include 
#include 

using namespace std;

int main()
{
    string s = "1,0";
    string result;

    //result.resize(s.length());
    replace_copy(s.begin(), s.end(), result.begin(), ',', '.');

    cout << '"' << result         << '"' << endl;
    cout << '"' << result.c_str() << '"' << endl;
    cout << result.length() << endl;

    return 0;
}

result.resize取消注释行的此程序的控制台输出是:

"1.0"
"1.0"
3

-thats好的,但是当line with result.resize被注释掉时,输出是:

""
"1.0"
0

- 这会导致奇怪的错误,因为result != result.c_str()!!!

这种行为replace_copy(以及可能也是类似的模板)可能会被视为标准库中的错误吗?我找不到任何与此主题相关的内容.谢谢.

编译器:mingw32-g ++ 4.7.1

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有