c++ - 关于字符串迭代器的问题

标签 c++

我有以下代码

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main(){
  string d="georgia is nice country":
  string::iterator my;
  for (my=d.begin();my!=d.end();my++) {
    cout<<*my<<endl;
  }
  return 0;
}

但它会导致编译器错误,提示“my is undefined”。怎么了?

最佳答案

你需要在字符串后面放一个分号而不是冒号。

string d="georgia is nice country"; // <-- semicolon!

关于c++ - 关于字符串迭代器的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3218304/

相关文章:

c++ - 使用 >> 运算符重载时出错

c++ - 是否可以创建一个宏来在 C/C++ 中创建一致的函数声明?

c++ - 如何在类的方法参数中使用模板

c++ - 在 gcc 中是否有字符串文字到 bool 转换的警告

c++ - 引用二维 vector 中的元素 (c++)

c++ - 返回类型为 T 的函数模板无法编译

c++ - LNK4098 - 它有多危险?

c++ - VS代码: How to add include path of project subDirectory?

c++ - 从 WPF 窗口内的托管 DLL 启动 WNDProc

c++ - 非默认运算符 <=> 在 C++20 中不会生成 == 和 !=