2012-02-24 22 views
137

如何對C++中的每個字符進行for循環?對於字符串中的每個字符

+4

什麼樣的繩子嗎? C字符串,還是'std :: string'? – Mysticial 2012-02-24 21:20:43

+0

它是從文本文件中讀取的,所以我假設std :: – 2012-02-24 21:22:15

+3

什麼樣的字符? 'char',Unicode代碼點,擴展字形集羣? – Philipp 2012-02-24 21:26:58

回答

268
  1. 循環通過std::stringcharacters,使用範圍爲基礎的環路(它從C++ 11'S,ALR伊迪支持近GCC,鐺的版本,和VC11測試版):

    std::string str = ???; 
    for(char& c : str) { 
        do_things_with(c); 
    } 
    
  2. 通過std::string的字符循環迭代器:

    std::string str = ???; 
    for(std::string::iterator it = str.begin(); it != str.end(); ++it) { 
        do_things_with(*it); 
    } 
    
  3. 通過std::string與字符循環一個老式的for循環:

    for(std::string::size_type i = 0; i < str.size(); ++i) { 
        do_things_with(str[i]); 
    } 
    
  4. 循環遍歷空字符轉換後的字符數組:

    char* str = ???; 
    for(char* it = str; *it; ++it) { 
        do_things_with(*it); 
    } 
    
+0

這有點讓我困惑,因爲std :: string是UTF8(假定是),所以編碼可能是可變長度的。你在迭代字符串中的字節還是字符? – Robinson 2014-07-25 09:56:15

+0

'std :: string'(被定義爲)'char'對象的空終止序列。 – 2014-07-25 10:37:00

+4

@Robinson:這是一個錯誤的假設。一個非常錯誤的假設。另外,「角色」有很多不同的含義,最好嚴格避免這個詞。 – Puppy 2014-07-25 10:43:58

4
const char* str = "abcde"; 
int len = strlen(str); 
for (int i = 0; i < len; i++) 
{ 
    char chr = str[i]; 
    //do something.... 
} 
+3

(過時的評論,這仍然可能與OP相關:)在循環條件中使用'strlen'不被認爲是好的形式,因爲每次迭代都需要對字符串進行O(n)操作,從而使整個循環O(n^2)的字符串的大小。如果字符串在循環期間發生變化,則可以調用循環條件中的'strlen',但應該保留用於實際* required *的情況。 – 2012-02-24 21:27:50

+0

@MagnusHoff:Yup,[畫家Schlemiel](http://en.wikipedia.org/wiki/Schlemiel_the_Painter%27s_algorithm)又一次提高了他醜陋的頭腦。 – 2012-02-24 21:30:01

+0

我編輯了我的答案。 Magnus你是對的,在過去的幾年中oops已經在c#中使用foreach;) – demoncodemonkey 2012-02-24 21:30:02

17

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++字符串,很容易處理。

18

在現代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。

+0

下面是部分C++ 11支持的編譯器的幾個選項:http://pastebin.com/LBULsn76 – 2012-02-24 21:34:16

+0

@BenjaminLindley:謝謝! 'auto'總是一個好主意。當使用它時,'begin()'和'cbegin()'之間的區別變得相關。 – 2012-02-24 21:43:42

+0

char(char&c)中的引用的作用是什麼?只是在需要的情況下允許修改字符值? – LunaticSoul 2015-06-02 16:03:46

0

對於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; 
} 
0
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,基本上是一個字符數組,因此您可以指定索引獲得字符。如果你不知道索引,那麼你可以像上面的代碼一樣遍歷它,但是當你進行比較時,確保使用單引號(它指定了一個字符)。

除此之外,上面的代碼是自我解釋的。

-2

您可以通過使用在字符串庫的功能獲得字符串中的每個字符,像我一樣像這樣

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)

2

這是另一種使用標準算法的方法。

#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; 
    }); 
} 
1

我沒有看到任何使用範圍爲基礎的循環與「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); 
} 
相關問題