什么是std::string...?怎么用?

什么是std::string...?怎么用?,第1张

std::string是标准C++的字符实现。为了让程序好移植,要用std::string。比如:

方法1:

#include <string>

std::string

方法2:

#include <string>

using namespace std

string

string类的构造函数:

string(const char *s)   //用c字符串s初始化

string(int n,char c)    //用n个字符c初始化

此外,string类还支持默认构造函数和复制构造函数,如string s1;string s2="hello";都是正确的写法。当构造的string太长而无法表达时会抛出length_error异常 ;

扩展资料

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的特性描述:

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填充不足的部分

你需要包含map,即#include <map>,以及tstring和ImageInfo的头文件,这个就你得自己来了。

这个是一个map,可以理解为“字典”容器,对于你那羡辩个m_map来说,里面的一个tstring“键Key”对应着一个ImageInfo“值Value”,所有的tstring都是不相同的,中族并且都是按照字符串顺序排列的。一般来说这个容器是使用红黑树来实现的。

常用的方法有insert(尝试插入新的键值对),begin/end(返回首尾迭代器),find(根据tstring查找某个匹配的元素),卖派弊erase(删除某些元素)这些。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12162013.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-21
下一篇 2023-05-21

发表评论

登录后才能评论

评论列表(0条)

保存