我是靠谱客的博主 彪壮外套,最近开发中收集的这篇文章主要介绍C++遍历字符串,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1. 常规方式

string s="hello"
for(int i=0;i<s.size();i++){
    cout<<s[i]<<"";
}

2.迭代器方式

begin接口 :         iterator begin();

end 接口:        iterator end();

循环方法

string s = "hello";
string::iterator it = s.begin();
while (it != s.end()) {
	cout << *it << "";
	++it;
}

3. 新for循环(C++11)

1.只读: for(const auto& 变量: 需要遍历的容器)

2.可读可写: for(auto &变量: 需要遍历的容器)

string s = "hello";
for (const auto& ch : s) {
	cout << c << "";//只读
}

for (auto& c : s) {
	c = 'a';// 可读可写
}

最后

以上就是彪壮外套为你收集整理的C++遍历字符串的全部内容,希望文章能够帮你解决C++遍历字符串所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(79)

评论列表共有 0 条评论

立即
投稿
返回
顶部