gpt4 book ai didi

c++ - 将迭代器存储到字符串中(转换、转换、追加?)

转载 作者:行者123 更新时间:2023-11-28 02:32:32 24 4
gpt4 key购买 nike

我正在尝试将一个字符串逐个字符地复制到另一个字符串中。目的不是复制整个字符串,而是复制其中的一部分(我稍后会为此做一些条件......)

但我不知道如何使用迭代器

你能帮帮我吗?

std::string str = "Hello world";
std::string tmp;

for (std::string::iterator it = str.begin(); it != str.end(); ++it)
{
tmp.append(*it); // I'd like to do something like this.
}

最佳答案

为什么不使用 + 运算符来连接字符串:

#include <iostream>
#include <sstream>
using namespace std;
int main(void)
{
string str = "Hello world";
string tmp = "";

for (string::iterator it = str.begin(); it != str.end(); ++it)
{
tmp+=(*it); // I'd like to do something like this.
}
cout << tmp;
getchar();
return (0);
}

关于c++ - 将迭代器存储到字符串中(转换、转换、追加?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28579013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com