📜  C++中的std :: basic_string ::运算符[]

📅  最后修改于: 2021-05-30 18:54:17             🧑  作者: Mango

返回对指定位置pos处字符的引用。不执行边界检查。如果pos> size(),则行为未定义。

句法 :

reference operator[] (size_type pos);
const_reference operator[] (size_type pos) const;
Parameters :
pos - position of the character to return
Return value :
Reference to the requested character
Exceptions :
No exception is thrown
/* CPP program to access
   a character through
   std::basic_string::operator[] */
#include 
  
//Driver code
int main()
{
    //String with valid indices from 0 to 2
    std::string str = "abc";
  
    //Printing size of string
    std::cout << "string size = " << str.size() << '\n';
      
    //Accessing element at index 2
    std::cout << "Element : " << str[2];
}

输出

string size = 3
Element : c
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”