如何對C++中的每個字符進行for循環?對於字符串中的每個字符
回答
循環通過
std::string
的characters,使用範圍爲基礎的環路(它從C++ 11'S,ALR伊迪支持近GCC,鐺的版本,和VC11測試版):std::string str = ???; for(char& c : str) { do_things_with(c); }
通過
std::string
的字符循環迭代器:std::string str = ???; for(std::string::iterator it = str.begin(); it != str.end(); ++it) { do_things_with(*it); }
通過
std::string
與字符循環一個老式的for循環:for(std::string::size_type i = 0; i < str.size(); ++i) { do_things_with(str[i]); }
循環遍歷空字符轉換後的字符數組:
char* str = ???; for(char* it = str; *it; ++it) { do_things_with(*it); }
const char* str = "abcde";
int len = strlen(str);
for (int i = 0; i < len; i++)
{
char chr = str[i];
//do something....
}
(過時的評論,這仍然可能與OP相關:)在循環條件中使用'strlen'不被認爲是好的形式,因爲每次迭代都需要對字符串進行O(n)操作,從而使整個循環O(n^2)的字符串的大小。如果字符串在循環期間發生變化,則可以調用循環條件中的'strlen',但應該保留用於實際* required *的情況。 – 2012-02-24 21:27:50
@MagnusHoff:Yup,[畫家Schlemiel](http://en.wikipedia.org/wiki/Schlemiel_the_Painter%27s_algorithm)又一次提高了他醜陋的頭腦。 – 2012-02-24 21:30:01
我編輯了我的答案。 Magnus你是對的,在過去的幾年中oops已經在c#中使用foreach;) – demoncodemonkey 2012-02-24 21:30:02
for循環可以實現這樣的:
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++ string)。我會建議使用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
,並for (char const & c : s)
或只是C++中的for (char c : s)
11。
下面是部分C++ 11支持的編譯器的幾個選項:http://pastebin.com/LBULsn76 – 2012-02-24 21:34:16
@BenjaminLindley:謝謝! 'auto'總是一個好主意。當使用它時,'begin()'和'cbegin()'之間的區別變得相關。 – 2012-02-24 21:43:42
char(char&c)中的引用的作用是什麼?只是在需要的情況下允許修改字符值? – LunaticSoul 2015-06-02 16:03:46
對於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;
}
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
}
//...........
}
一個String,基本上是一個字符數組,因此您可以指定索引獲得字符。如果你不知道索引,那麼你可以像上面的代碼一樣遍歷它,但是當你進行比較時,確保使用單引號(它指定了一個字符)。
除此之外,上面的代碼是自我解釋的。
您可以通過使用在字符串庫的功能獲得字符串中的每個字符,像我一樣像這樣
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)
這是另一種使用標準算法的方法。
#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;
});
}
我沒有看到任何使用範圍爲基礎的循環與「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數組例如
int ia[] = {1,2,3,4,5,6};
for (int& i : ia) {
printf("%d", i);
}
- 1. 基於每個字符串
- 2. 乳膠 - 對字符串中的每個字符應用操作
- 3. Python:Split字符串中的每n個字的分割字符串
- 4. 如何對齊字符串的字段列中的每個字符串
- 5. 用於顯示字符串|的每個字符的循環C++
- 6. 分割字符串,每5個字符
- 7. 拆分字符串每n個字符
- 8. 將每個對象中的字符串循環到一個字符串
- 9. JSON字符串解析每個字符作爲一個對象
- 10. PHP中的錯誤對於每個字符串
- 11. 對於受保護字符串函數中的每個循環
- 12. 對於數組中的每個字符串
- 13. jQuery的每個字符串
- 14. 反轉字符串結構中的每個字符串
- 15. 字符串數組增加TextToSpeech中的每個字符串
- 16. 用於Java中大字符串的唯一字符串對象
- 17. 用另一個字符串中的每個字符替換字符串中的字符
- 18. 字符串中的每一個字前後加字符
- 19. 用隨機字符替換字符串中的每個數字
- 20. 將字符串中的每個字符轉換爲字典鍵
- 21. 計算字符串數組中的每個字母字符Java
- 22. 爲每個字符串添加特定字符python中的字符串列表
- 23. 調用每個字符串對象
- 24. TSQL UDF來分割字符串中的每個8個字符
- 25. 返回字符串中每個單詞的第一個字符
- 26. 將char放入每個N個字符的java字符串中
- 27. JavaScript代碼追加一個字符串中的每個字符
- 28. c#對於字符串中的每7個字母,將項目字符串添加到列表框
- 29. 爲什麼字符串的每個單個字符都不等於字符對等?
- 30. 在字符串數組中的所有字符串的每個字符之間插入一個字符
什麼樣的繩子嗎? C字符串,還是'std :: string'? – Mysticial 2012-02-24 21:20:43
它是從文本文件中讀取的,所以我假設std :: – 2012-02-24 21:22:15
什麼樣的字符? 'char',Unicode代碼點,擴展字形集羣? – Philipp 2012-02-24 21:26:58