本篇的内容是记录使用string函数的测试与使用,方便后续使用时查阅使用
//引用头文件和展开命名空间
#include
#include
using namespace
string::resize的使用
void resize (size_t n);
void resize (size_t n, char c);
如果 n 小于当前字符串长度,则当前值将缩短为其前 n 个字符,并删除第 n 个字符以外的字符。
如果 n 大于当前字符串长度,则通过在末尾插入任意数量的字符来扩展当前内容,以达到 n 的大小。
如果指定了 c,则新元素将初始化为 c 的副本,否则,它们是值初始化的字符(null 字符)。
void Teststring1()
{
// 注意:string类对象支持直接用cin和cout进行输入和输出
string s("hello, zjc!!!");
cout << s.size() << endl;
cout << s.length() << endl;
cout << s.capacity() << endl;
cout << s << endl;
// 将s中的字符串清空,注意清空时只是将size清0,不改变底层空间的大小
s.clear();
cout << s.size() << endl;
cout << s.capacity() << endl;
// 将s中有效字符个数增加到10个,多出位置用'a'进行填充
// “aaaaaaaaaa”
s.resize(10, 'a');
cout << s.size() << endl;
cout << s.capacity() << endl;
//将s中有效字符个数增加到15个,多出位置用缺省值'\0'进行填充
//”aaaaaaaaaaa\0\0“
//注意此时s中有效个数已经增加到15个
s.resize(15);
cout << s.size() << endl;
cout << s.capacity() << endl;
cout << s << endl;
//将s中有效数字符缩小到5个
s.resize(5);
cout << s.size() << endl;
cout << s.capacity() <erase()函数用于从字符串中删除指定位置或指定范围的字符。它可以接受一个参数或两个参数。
1.当传递一个参数时,表示从指定位置开始删除到字符串的末尾的所有字符。
2.当传递两个参数时,表示从指定位置开始删除指定数量的字符。
3.调用erase()后,字符串的长度会相应地减少,并且内存空间也可能会被重新分配以适应新的长度。
void Teststring2()
{
//clear()函数用于清空字符串的内容,将字符串变为空字符串,即不包含任何字符。
//调用clear()后,字符串的长度将变为0,但内存空间不会被释放,仍然保留着。
string s1("hello wolrd");
s1.clear();
cout << "s1: " << s1 <void Teststring3()
{
string s;
//测试reserve是否改变string中有效元素个数
s.reserve(100);
cout << s.size()<void Teststring4()
{
string s1("hello world");
const string s2("hello World");
cout << s1 << " " << s2 << endl;
cout << s1[0] << " "<< s2[0] << endl;
s1[0] = 'H';
cout << s1 << endl;
//s2[0] = 'H'; //代码编译失败,因为const类型对象不能修改
}
void Teststring5()
{
string s("hello world");
// 3种遍历方式:
//需要注意的以下三种方式除了遍历string对象,还可以遍历修改string中的字符,
// 另外以下三种方式对于string而言,第一种使用最多
//1.for + operator[]
for(size_t i = 0; i< s.size();++i)
cout << s[i] <<" ";
cout << endl;
//2.迭代器
string::iterator it = s.begin();
while(it != s.end())
{
cout << *it << " ";
++it;
}
cout << endl;
// string::reverse_iterator = s.rbegin();
//C++11之后。直接使用auto定义迭代器,让编译器推导迭代器的类型
auto rit = s.rbegin();
while(rit != s.rend())
{
cout << *rit << endl;
rit++;
}
//3.范围for
for(auto ch : s) //如果要修改的话,使用引用
cout << ch << " ";
cout << endl;
}
string::substr
生成子字符串
返回一个新构造的对象,其值初始化为此对象的子字符串的副本。子字符串是对象的一部分,它从字符位置开始并跨越字符(或直到字符串的末尾,以先到者为准)。
string::npos;
size_t的最大值。NPOS 是一个静态成员常量值,具有 size_t 类型元素的最大可能值。当此值用作字符串成员函数中 len(或 sublen)参数的值时,表示“直到字符串末尾”。作为返回值,它通常用于指示不匹配。此常量使用值 -1 定义,由于 size_t 是无符号整数类型,因此它是此类型的最大可能可表示值。
}
void Teststring6()
{
string str;
str.push_back(' '); // 在str后插入空格,这里要用单引号
str.append("hello "); // 在str后追加一个字符“hello”
str += 'w'; // 在str后追加一个字符'b'
str += "orld";
cout << str << endl;
cout << str.c_str() << endl; //以c语言的方式打印字符串
// 获取file的后缀
string file("string.cpp");
size_t pos = file.rfind('.');// 从后往前找
string suffix(file.substr(pos,file.size()-pos));
//pos: 遇到'.'字符位置,len=file.size()-pos 要生成子字符串长度
cout << suffix << endl;
//nops是是string里面的一个静态成员变量
//static const size_t npos = -1;
//取出ur1中的域名
string ur1("http://www.cplusplus.com/reference/string/string/find/");
cout << ur1 << endl;
size_t start = 0;
size_t finish = ur1.find("://");
if(start == string::npos)
{
cout << "invalid ur1" << endl;
return;
}
// string substr (size_t pos = 0, size_t len = npos) const;
string address = ur1.substr(start,finish - start);
cout << address << ' ';
start += address.size()+3;
do
{
finish = ur1.find('/',start);
address = ur1.substr(start,finish - start);
cout << address << ' ';
start += address.size()+1;
}while(finish!= (ur1.size()-1));
cout << endl;
// 删除ur1的协议前缀
pos = ur1.find("://");
ur1.erase(0, pos + 3);
cout << ur1 <int main()
{
Teststring5();
//TestPushBackReserve();
return 0;
}