文章目录
前言:
std::string
是 C++ 标准库的一部分,但它不是 STL 容器的一部分。STL 容器是指那些基于模板的容器,如std::vector
、std::list
等。std::string
提供了类似于 STL 容器的功能,比如动态内存管理、迭代器支持等,但它的设计和实现是独立的。std::string
被归为 STL 是因为它具有类似于 STL 容器的特性和功能,尽管它在技术上并不属于 STL。这种归类更多是出于功能和使用上的相似性,而不是严格的分类。std::string
是 C++ 标准库中的一个类,它提供了一种方便的方式来创建、操作和处理字符串。
1. string的介绍
std::string
在 C++ 中是一个非常重要的概念,它代表了字符串(String)类型,用于处理文本数据。字符串是由字符(通常是字母、数字、标点符号等)组成的序列。在 C++ 中,std::string
是一个类,它封装了字符串的存储和操作,提供了一系列的成员函数和操作符重载来方便地进行字符串操作。
2. string类对象的常见构造
// string constructor
#include <iostream>
#include <string>
int main ()
{
std::string s0 ("Initial string");
// constructors used in the same order as described above:
std::string s1;
std::string s2 (s0);
std::string s3 (s0, 8, 3);
std::string s4 ("A character sequence");
std::string s5 ("Another character sequence", 12);
std::string s6a (10, 'x');
std::string s6b (10, 42); // 42 is the ASCII code for '*'
std::string s7 (s0.begin(), s0.begin()+7);
std::cout << "s1: " << s1 << "\ns2: " << s2 << "\ns3: " << s3;
std::cout << "\ns4: " << s4 << "\ns5: " << s5 << "\ns6a: " << s6a;
std::cout << "\ns6b: " << s6b << "\ns7: " << s7 << '\n';
return 0;
}
std::string s1;
:这是默认构造函数,创建了一个空的字符串对象s1
。std::string s2(s0);
:这是复制构造函数,创建了一个新字符串对象s2
,它是另一个字符串对象s0
的副本。std::string s3(s0, 8, 3);
:这是带参数的构造函数,创建了一个新字符串对象s3
,它从s0
的第8个字符开始(注意C++中字符串索引从0开始),提取长度为3的子字符串。std::string s4("A character sequence");
:这是从C风格字符串构造函数,创建了一个新字符串对象s4
,初始化为给定的C风格字符串。std::string s5("Another character sequence", 12);
:这是带长度参数的构造函数,创建了一个新字符串对象s5
,初始化为给定的C风格字符串的前12个字符。std::string s6a(10, 'x');
:这是从字符和重复次数构造函数,创建了一个新字符串对象s6a
,包含10个字符’x’。std::string s6b(10, 42);
:这也是从字符和重复次数构造函数,创建了一个新字符串对象s6b
,包含10个字符,其ASCII码为42,对应字符’*'。std::string s7(s0.begin(), s0.begin() + 7);
:这是从迭代器范围构造函数,创建了一个新字符串对象s7
,包含从s0
的开始迭代器到第7个元素的子字符串。
3. string类对象的容量操作
// string::size
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
std::cout << "The size of str is " << str.size() << " bytes.\n";
return 0;
}
// string::length
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
std::cout << "The size of str is " << str.length() << " bytes.\n";
return 0;
}
// comparing size, length, capacity and max_size
#include <iostream>
#include <string>
int main ()
{
std::string str ("Test string");
std::cout << "size: " << str.size() << "\n";
std::cout << "length: " << str.length() << "\n";
std::cout << "capacity: " << str.capacity() << "\n";
std::cout << "max_size: " << str.max_size() << "\n";
return 0;
}
// resizing string
#include <iostream>
#include <string>
int main ()
{
std::string str ("I like to code in C");
std::cout << str << '\n';
unsigned sz = str.size();
str.resize (sz+2,'+');
std::cout << str << '\n';
str.resize (14);
std::cout << str << '\n';
return 0;
}
// string::reserve
#include <iostream>
#include <fstream>
#include <string>
int main ()
{
std::string str;
std::ifstream file ("test.txt",std::ios::in|std::ios::ate);
if (file) {
std::ifstream::streampos filesize = file.tellg();
str.reserve(filesize);
file.seekg(0);
while (!file.eof())
{
str += file.get();
}
std::cout << str;
}
return 0;
}
// string::clear
#include <iostream>
#include <string>
int main ()
{
char c;
std::string str;
std::cout << "Please type some lines of text. Enter a dot (.) to finish:\n";
do {
c = std::cin.get();
str += c;
if (c=='\n')
{
std::cout << str;
str.clear();
}
} while (c!='.');
return 0;
}
// string::empty
#include <iostream>
#include <string>
int main ()
{
std::string content;
std::string line;
std::cout << "Please introduce a text. Enter an empty line to finish:\n";
do {
getline(std::cin,line);
content += line + '\n';
} while (!line.empty());
std::cout << "The text you introduced was:\n" << content;
return 0;
}
// string::shrink_to_fit
#include <iostream>
#include <string>
int main ()
{
std::string str