// Practice5_string.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <string> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { /* 字符的遍历 char* cArray = "2017/3/2 22:40"; string str(cArray); //数组方式 for(unsigned int i = 0; i < str.size(); i++) { cout << str[i] << " "; } cout << endl; //正向迭代器方式 string::iterator it = str.begin(); for(; it != str.end(); it++) { cout << *it << " "; } cout << endl; //反向迭代器方式 string::reverse_iterator itReverse = str.rbegin(); for(; itReverse != str.rend(); itReverse++) { cout << *itReverse << " "; } cout << endl; */ string str("dog bird cat elephant chicken"); //查找字符串 cout << str.find("bird") << endl; cout << (int)str.find("pig") << endl; //查找字符 cout << str.find('i', 7) << endl; //从末尾查找字符串 cout << str.rfind("cat") << endl; //从末尾查找字符 cout << str.rfind('c') << endl; //查找第一个属于某子串的字符 cout << str.find_first_of("23d56") << endl; //查找第一个不属于某子串的字符 cout << str.find_first_not_of("dog bird 2017") << endl; //查找最后一个属于某子串的字符 cout << str.find_last_of("23d56") << endl; //查找最后一个不属于某子串的字符 cout << str.find_last_not_of("tea") << endl; return 0; }
运行结果:
4
-1
24
9
25
0
9
7
28