将迭代器存储到字符串中(Conversion、cast、append?)
Storing an iterator into a string (Conversion, cast, append ?)
我正试图逐个字符地将字符串复制到另一个字符中。目的不是复制整个字符串,而是只复制其中的一部分(我稍后会为此做一些条件。)
但是我不知道如何使用iterators
。
你能帮我吗?
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);
}
你可以试试这个:
std::string str = "Hello world";
std::string tmp;
for (std::string::iterator it = str.begin(); it != str.end(); ++it)
{
tmp += *it;
}
cout << tmp;
相关文章:
- 如何理解C++标准N3337中的expr.const.cast子句8
- C++Cast运算符过载
- 错误:"cast"未命名类型void setCastDescription(std::string
- 通过使用 const-cast 的非常量引用来延长临时的寿命
- "(void) cast"与功能有什么区别 "__attributes__"来沉默未使用的参数警告?
- 指向字符串和 appEnd() 方法的指针的 C++ 数组
- 具有QList功能的QT应用程序<QString>"append"
- C++:"Expected '(' for function-style cast or type construction"错误
- 为什么选择 g++ 给予者:"error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]"
- QTextStream 无法使用开放模式写入文件,包括"t" QIODevice::Append
- Gtk+ g_signal_connect() 和 C++ lambda 会导致"invalid cast"错误
- Shared_ptr cast vs static_cast speed
- c++ std::bad_alloc on std::filesystem::path append
- C2664 :当我使用 str.append(str[i]) 时,发生错误
- 了解 QVector append() - 为什么它会有条件地复制
- QUrlQuery append?
- 在 iOS 上使用 Aruco 构建 OpenCV 时"Functional-style cast from id to double is not allowed"
- 覆盖 CAST 运算符(我认为它被称为向下转换)
- C++错误,隐 <function-style-cast> 式要求使用模板化类一次调用多个构造函数的多个转换
- 将迭代器存储到字符串中(Conversion、cast、append?)