MemoryGarden's Blog

努力 -----------大能猫

C++博客 首页 新随笔 联系 聚合 管理
  118 Posts :: 11 Stories :: 20 Comments :: 0 Trackbacks
string::at

This member function behaves as operator[] , except that at also performs a range check, throwing an exception of type out_of_range in case that pos is not an actual position in the string

和[]运算符不同的地方是当它接受一个非法的参数的时候,会抛出异常


1 const char& at ( size_t pos ) const;
2       char& at ( size_t pos );


 1 // string::at
 2 #include <iostream>
 3 #include <string>
 4 using namespace std;
 5 
 6 int main ()
 7 {
 8   string str ("Test string");
 9   int i;
10   for (i=0; i < str.length(); i++)
11   {
12     cout << str.at(i);
13   }
14   return 0;
15 }

posted on 2009-12-28 01:52 memorygarden 阅读(443) 评论(0)  编辑 收藏 引用 所属分类: STL

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理