C++ 对于字符串中的每个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9438209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
For every character in string
提问by Hyman Wilsdon
How would I do a for loop on every character in string in C++?
我将如何对 C++ 中字符串中的每个字符执行 for 循环?
回答by R. Martinho Fernandes
Looping through the charactersof 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
,使用范围为基础的环路(它是从C ++ 11,近GCC,铛的版本已经支撑,并且VC11测试版):std::string str = ???; for(char& c : str) { do_things_with(c); }
std::string
使用迭代器循环遍历 a 的字符:std::string str = ???; for(std::string::iterator it = str.begin(); it != str.end(); ++it) { do_things_with(*it); }
std::string
用老式的 for循环遍历 a 的字符:std::string str = ???; for(std::string::size_type i = 0; i < str.size(); ++i) { do_things_with(str[i]); }
循环遍历以 null 结尾的字符数组的字符:
char* str = ???; for(char* it = str; *it; ++it) { do_things_with(*it); }
回答by Hyman Wilsdon
A for loop can be implemented like this:
for 循环可以这样实现:
string str("HELLO");
for (int i = 0; i < str.size(); i++){
cout << str[i];
}
This will print the string character by character. str[i]
returns character at index i
.
这将逐字符打印字符串。str[i]
返回 index 处的字符i
。
If it is a character array:
如果是字符数组:
char str[6] = "hello";
for (int i = 0; str[i] != 'std::string s("Hello world");
for (char & c : s)
{
std::cout << "One character: " << c << "\n";
c = '*';
}
'; i++){
cout << str[i];
}
Basically above two are two type of strings supported by c++. The second is called c string and the first is called std string or(c++ string).I would suggest use c++ string,much Easy to handle.
基本上上面两种是c++支持的两种类型的字符串。第二个称为 c 字符串,第一个称为 std 字符串或(c++ 字符串)。我建议使用 c++ 字符串,很容易处理。
回答by Kerrek SB
In modern C++:
在现代 C++ 中:
for (std::string::iterator it = s.begin(), end = s.end(); it != end; ++it)
{
std::cout << "One character: " << *it << "\n";
*it = '*';
}
In C++98/03:
在 C++98/03 中:
#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;
});
}
For read-only iteration, you can use std::string::const_iterator
in C++98, and for (char const & c : s)
or just for (char c : s)
in C++11.
对于只读迭代,您可以std::string::const_iterator
在 C++98 中使用,for (char const & c : s)
或仅for (char c : s)
在 C++11 中使用。
回答by 0xBADF00
Here is another way of doing it, using the standard algorithm.
这是使用标准算法的另一种方法。
const char* str = "abcde";
int len = strlen(str);
for (int i = 0; i < len; i++)
{
char chr = str[i];
//do something....
}
回答by demoncodemonkey
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);
}
回答by 10SecTom
I don't see any examples using a range based for loop with a "c string".
我没有看到任何使用基于范围的带有“c 字符串”的 for 循环的示例。
int ia[] = {1,2,3,4,5,6};
for (int& i : ia) {
printf("%d", i);
}
not related but int array example
不相关但 int 数组示例
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
}
//...........
}
回答by almost a beginner
char mystring[] = "My String";
int size = strlen(mystring);
int i;
for(i = 0; i < size; i++) {
char c = mystring[i];
}
A String is basically an array of characters, therefore you can specify the index to get the character. If you don't know the index, then you can loop through it like the above code, but when you're making a comparison, make sure you use single quotes (which specifies a character).
String 基本上是一个字符数组,因此您可以指定索引来获取字符。如果您不知道索引,那么您可以像上面的代码一样循环遍历它,但是在进行比较时,请确保使用单引号(指定一个字符)。
Other than that, the above code is self explanatory.
除此之外,上面的代码是不言自明的。
回答by Tiago Pasqualini
For C-string (char []
) you should do something like this:
对于 C-string ( char []
) 你应该做这样的事情:
std::string mystring = "My String";
std::string::iterator it;
for(it = mystring.begin(); it != mystring.end(); it++) {
char c = *it;
}
For std::string
you can use str.size()
to get its size and iterate like the example , or could use an iterator:
因为std::string
您可以使用str.size()
获取其大小并像示例一样进行迭代,或者可以使用迭代器:
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;
}
回答by Shuja Ul Hasan
you can get every char in a string by using the at function of string library, like i did it like this
您可以使用字符串库的 at 函数获取字符串中的每个字符,就像我这样做一样
##代码##this is just a segment of my code but the point is you can access characters by stringname.at(index)
这只是我代码的一部分,但关键是您可以通过以下方式访问字符 stringname.at(index)