C++ get(ch)上at()函数的必要性
当我们已经有get(ch)/get()/put()返回对指定位置字符的引用时,使用at()函数的必要性是什么C++ get(ch)上at()函数的必要性,c++,C++,当我们已经有get(ch)/get()/put()返回对指定位置字符的引用时,使用at()函数的必要性是什么 // string::at //following is a code that explains the use of at() function #include <iostream> //header file-iostream #include <string.h> //header file -string.h,whic
// string::at
//following is a code that explains the use of at() function
#include <iostream> //header file-iostream
#include <string.h> //header file -string.h,which contains all the string related functions
using namespace std; //for using cout & cin
int main ()
{
string str ("Test string"); //initialize str
for (unsigned i=0; i<str.length(); ++i) //for loop for printing the string stored in str
{
cout << str.at(i); //displaying str using at() function
}
return 0; //nothing is returned
}
//字符串::at
//下面是解释at()函数用法的代码
#include//头文件iostream
#包括//头文件-string.h,其中包含所有与字符串相关的函数
使用名称空间std//使用cout&cin
int main()
{
字符串str(“测试字符串”);//初始化str
对于(unsigned i=0;i的点是,允许您访问字符串的单个字符和指定的索引。如果索引提供字符串的=
大小()
,则将引发异常
这个例子只是演示了个人访问
std::cout << str;
std::这些函数能做完全不同的事情吗?在你的例子中,你能分享一下你认为可以替代at
的方法吗?at()函数和get(ch)都是用来访问字符串的,一个字符接一个字符,那么使用at()函数替代get(ch)有什么必要呢在中,只有<代码>实际上是<代码> STD::String 中的成员。那么其他人应该如何访问给定字符串的字符?<代码>字符串和字符串。H</C>是完全不同的标题!实际上,在C++标准中没有<代码>字符串。