天天看點

C++中如何通路字元串中的單個字元通路字元串中的單個字元

通路字元串中的單個字元

通路字元串中的單個字元是像字元數組那樣使用下标通路字元串中的某個元素。

#include <iostream>

int main(int argc,const char* argv[])
{
	std::string str1 = "Hello World!";

	//向通路字元數組那樣,将字元串中的第二個字元提取出來顯示,如果下标越界,不會報告錯誤。
	std::cout << str1[2] << std::endl;

	//使用成員函數來獲得字元串中的第6個字元并顯示,如果參數越界,函數将抛出異常。
	std::cout << str1.at(6) << std::endl;

	return 0;
}
           

繼續閱讀