我该如何在C++中对字符串中的每个字符进行for循环?
Looping through the characters of a std::string
, using a range-based for loop (it's from C++11, already supported in recent releases of GCC, clang, and the VC11 beta):
std::string str = ???;
for(char& c : str) {
do_things_with(c);
}
Looping through the characters of a std::string
with iterators:
std::string str = ???;
for(std::string::iterator it = str.begin(); it != str.end(); ++it) {
do_things_with(*it);
}
Looping through the characters of a std::string
with an old-fashioned for-loop:
std::string str = ???;
for(std::string::size_type i = 0; i < str.size(); ++i) {
do_things_with(str[i]);
}
Looping through the characters of a null-terminated character array:
char* str = ???;
for(char* it = str; *it; ++it) {
do_things_with(*it);
}
std::string
之上的更高级别的抽象概念,而std::string
只是一系列字节。” - Lightness Races in Orbitfor循环可以这样实现:
string str("HELLO");
for (int i = 0; i < str.size(); i++){
cout << str[i];
}
str [i]
返回索引为i
的字符。char str[6] = "hello";
for (int i = 0; str[i] != '\0'; i++){
cout << str[i];
}
基本上,C++ 支持以上两种字符串类型。第二种称为 C 字符串,而第一种称为 std string 或(C++ 字符串)。我建议使用 C++ 字符串,更易于处理。
在现代的C++中:
std::string s("Hello world");
for (char & c : s)
{
std::cout << "One character: " << c << "\n";
c = '*';
}
在C++98/03中:
for (std::string::iterator it = s.begin(), end = s.end(); it != end; ++it)
{
std::cout << "One character: " << *it << "\n";
*it = '*';
}
对于只读迭代,你可以在C++98中使用std::string::const_iterator
,在C++11中可以使用for (char const & c : s)
或者for (char c : s)
。
auto
总是一个好主意。在使用它时,begin()
和cbegin()
之间的区别变得相关了。 - Kerrek SBchar & c
中的引用角色是什么?它只是允许在需要修改字符值的情况下进行修改吗? - LunaticSoul这里有另一种使用标准算法的方法。
#include <iostream>
#include <string>
#include <algorithm>
int main()
{
std::string name = "some string";
std::for_each(name.begin(), name.end(), [] (char c) {
std::cout << c;
});
}
const char* str = "abcde";
int len = strlen(str);
for (int i = 0; i < len; i++)
{
char chr = str[i];
//do something....
}
strlen
作为循环条件是不好的做法,因为它需要对字符串进行O(n)的操作,使整个循环在字符串大小上变为O(n^2)。如果字符串在循环中发生更改,则可以调用循环条件中的strlen
,但应将其保留用于实际需要的情况。请注意,这是过时的评论,但仍然可能与问题相关。 - Magnus Hoff我没有看到任何使用基于范围的for循环来处理"C字符串"的示例。
char cs[] = "This is a c string\u0031 \x32 3";
// range based for loop does not print '\n'
for (char& c : cs) {
printf("%c", c);
}
不相关但是整数数组示例
int ia[] = {1,2,3,4,5,6};
for (int& i : ia) {
printf("%d", i);
}
for (int x = 0; x < yourString.size();x++){
if (yourString[x] == 'a'){
//Do Something
}
if (yourString[x] == 'b'){
//Do Something
}
if (yourString[x] == 'c'){
//Do Something
}
//...........
}
字符串基本上是字符数组,因此您可以指定索引来获取字符。如果您不知道索引,则可以像上面的代码一样循环遍历它,但在进行比较时,请确保使用单引号(表示一个字符)。
除此之外,上面的代码是自说明的。
对于 C 字符串(char []
),你应该这样做:
char mystring[] = "My String";
int size = strlen(mystring);
int i;
for(i = 0; i < size; i++) {
char c = mystring[i];
}
对于std::string
,您可以使用str.size()
获取其大小并像示例一样进行迭代,或者可以使用迭代器:
std::string mystring = "My String";
std::string::iterator it;
for(it = mystring.begin(); it != mystring.end(); it++) {
char c = *it;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin >> s;
int length = s.size();
for(int i = 0; i < length; i++)
{
process(s[i]);
}
}
你可以使用字符串库的at函数来获取字符串中的每个字符,就像我这样做:
string words;
for (unsigned int i = 0; i < words.length(); i++)
{
if (words.at(i) == ' ')
{
spacecounter++; // to count all the spaces in a string
if (words.at(i + 1) == ' ')
{
i += 1;
}
这只是我的代码片段,但重点是您可以通过 stringname.at(index)
访问字符。
std::string
? - Mysticialchar
、Unicode 码点、扩展字形集群? - Philipp