c++ std::string 用法 |
您所在的位置:网站首页 › std::string用法 › c++ std::string 用法 |
std::string用法总结 在平常工作中经常用到了string类,本人记忆了不好用到了的时候经常要去查询。在网上摘抄一下总结一下,为以后的查询方便: string类的构造函数: string(const char *s); //用c字符串s初始化string(int n,char c); //用n个字符c初始化 string类的字符操作: const char &operator[](int n)const; const char &at(int n)const; char &operator[](int n); char &at(int n); operator[]和at()均返回当前字符串中第n个字符的位置,但at函数提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问。 const char *data()const;//返回一个非null终止的c字符数组 const char *c_str()const;//返回一个以null终止的c字符串 int copy(char *s, int n, int pos = 0) const;//把当前串中以pos开始的n个字符拷贝到以s为起始位置的字符数组中,返回实际拷贝的数目 注:对于string中对象字符的处理,有很多已有的函数在CCtype头文件中,可以很方便的应用 string的特性描述: int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数) int max_size()const; //返回string对象中可存放的最大字符串的长度 int size()const; //返回当前字符串的大小 int length()const; //返回当前字符串的长度 bool empty()const; //当前字符串是否为空 void resize(int len,char c);//把字符串当前大小置为len,并用字符c填充不足的部分 string类的输入输出操作:string类重载运算符operator>>用于输入,同样重载运算符operator时返回1,>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test" ostringstream os; os |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |