題目大意:AAA?BBB?CCC? 分紅:AAA BBB CCCios
樣例:ide
輸入: AAA?BBB?CCC? ? 2 (即輸入AAA?BBB?CCC?和?和2)spa
輸出:BBBcode
思路代碼:使用strtok截取字符串,並使用一個變量計數,將計數和截取的字符串裝入map中,進行遍歷便可。blog
1 #include <iostream> 2 #include <string.h> 3 #include <stdio.h> 4 #include <map> 5 using namespace std; 6 7 char strin[] = "AAA?BBB?CCC?"; 8 char seps[] = "?"; 9 char *token; 10 11 int main() 12 { 13 map<int ,char *> m; 14 printf( "%s\n\nTokens:\n", strin ); 15 token = strtok( strin, seps ); 16 int k = 1; 17 while( token != NULL ) 18 { 19 m.insert(pair<int,char *>(k++,token)); 20 token = strtok( NULL, seps ); 21 } 22 map<int,char *>::iterator it; 23 for(it = m.begin();it != m.end();it++) 24 { 25 if(it->first == 2) 26 { 27 cout<<it->first<<" "<<it->second<<endl; 28 } 29 30 } 31 }