C++——string使用

string的常见构造接口 

string()

构造空的srting类对象,空字符串

string(const char* str)

用字符串初始化

string(const string& str) 拷贝构造,使用string类初始化
string(size_t n, char c)  用n个字符c初始化
string s1; 
string s2("hello world");
string s3(s2); 
string s4(10,'*');

string类对象的容量操作

size 返回字符串有效长度
capacity 返回总空间大小
empty 判断对象是否为空
clear 清空有效字符
reserve 为字符串预留空间
resize 修改有效字符个数

size——返回字符串的有效长度

std::string::size
size_t size() const;
int main ()
{
  std::string str ("Test string");
  std::cout << "The size of str is " << str.size() << " bytes.\n";
  return 0;
}
//The size of str is 11 bytes

 capacity——返回空间总大小

std::string::capacity
size_t capacity() const;
int main ()
{
  std::string str ("Test string");
  std::cout << "size: " << str.size() << "\n";
  std::cout << "capacity: " << str.capacity() << "\n";
  return 0;
}

//size: 11
//capacity: 15

 empty——判断对象是否为空

std::string::empty
bool empty() const;

应用:将用户输入的内容逐行读取并存储,直到遇到空行 

#include<iostream>
#include<string>
using namespace std;
int main()
{
	string line;
	string content;
	do
	{
		getline(cin, line);
		content += line + '\n';
	} while (!line.empty());
	cout << content;
}

clear——清空有效字符

std::string::clear
void clear();
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str("hello world");
	str.clear();
	if (str.empty())
		cout << "string has been deleted" << endl;
}

clear只改清空数据,不改变空间大小 

reserve——为字符串预留空间

std::string::reserve
void reserve (size_t n = 0);
int main()
{
	string str("hello world");
	cout << str.capacity() << endl;
	cout << str.size() << endl;
	str.reserve(100);
	cout << str.capacity() << endl;
	cout << str.size() << endl;
}

 当参数n小于字符串大小时,不会缩小容量

resize——修改有效字符个数

std::string::resize
void resize (size_t n);void resize (size_t n, char c);

当n大于字符串大小时,多的空间会用字符c来填充(\0不会被覆盖) ,如果c缺省会自动补\0;如果n小于字符串大小时,会将多出的字符直接删除,但是不会改变基层空间capacity

int main()
{
	string str;
	str.resize(1);
	cout << str << endl;
	str.resize(5, '+');
	cout << str << endl;//字符串是 \0++++ 不同编译器输出可能不同
	str.resize(1);
	cout << str << endl;
}

string类对象的访问及遍历

operator[ ] 下标方式访问,类似数组
begin,end 迭代器顺序遍历
rbegin,rend 迭代器逆序遍历
范围for 底层还是迭代器
c_str 返回c格式字符串

operator[ ]——[ ]的运算符重载

std::string::operator[]
char& operator[] (size_t pos);const char& operator[] (size_t pos) const;

使用和数组类似,都是访问[ ]内数字下标位置的字符

int main()
{
	string str("hello world");
	for (int i = 0; i < str.size(); i++)
	{
		cout << str[i];
	}
	cout << endl;
}

迭代器beginend,顺序遍历

iterator begin();const_iterator begin() const;
iterator end();const_iterator end() const;
using namespace std;

int main()
{
	string str("hello world");
	string::iterator sit = str.begin();
	while (sit != str.end())
	{
		cout << *sit;
		sit++;
	}
	cout << endl;
}

迭代器rbeginrend,逆序遍历

reverse_iterator rbegin();const_reverse_iterator rbegin() const;
reverse_iterator rend();const_reverse_iterator rend() const;
using namespace std;

int main()
{
	const string str("hello world");
	string::const_reverse_iterator sit = str.rbegin();
	while (sit != str.rend())
	{
		cout << *sit;
		sit++;
	}
	cout << endl;
}

范围for(底层使用迭代器)

int main()
{
	const string str("hello world");
	for (char c : str)
	{
		cout << c;
	}
	cout << endl;
}

c_str——返回c格式字符串

std::string::c_str

const char* c_str() const;
int main()
{
	string str("hello world");
	const char* ch = str.c_str();
	cout << ch << endl;
}

 这个接口是为了和c语言更好兼容


string类对象的修改

append 追加字符串
operator+=

追加字符串

find 从前寻找字符(串)并返回下标位置
rfind 从后寻找字符(串)并返回下标位置
substr 从字符串中截取子串

append——追加字符串

std::string::append
	
//追加str
string& append (const string& str);
	
//追加str的子串,字串是从str的subpos下标位置开始,向后的sublen个字符
string& append (const string& str, size_t subpos, size_t sublen);

//追加常量字符串s
string& append (const char* s);
	
//追加常量字符串的前n个字符
string& append (const char* s, size_t n);
	
//追加n个字符c
string& append (size_t n, char c);

//迭代器追加
template <class InputIterator>   string& append (InputIterator first, InputIterator last);
#include <iostream>
#include <string>

int main ()
{
  std::string str;
  std::string str2="Writing ";
  std::string str3="print 10 and then 5 more";

  // used in the same order as described above:
  str.append(str2);                       // "Writing "
  str.append(str3,6,3);                   // "10 "
  str.append("dots are cool",5);          // "dots "
  str.append("here: ");                   // "here: "
  str.append(10,'.');                    // ".........."
  str.append(str3.begin()+8,str3.end());  // " and then 5 more"
  str.append(5,'.');                // "....."

  std::cout << str << '\n';
  return 0;
}
//Writing 10 dots here: .......... and then 5 more.....

operator+= ——追加字符串

std::string::operator+=
	
string& operator+= (const string& str);
	
string& operator+= (const char* s);
	
string& operator+= (char c);

+=运算符重载和append的功能基本相同,append除此之外可以使用迭代器,插入指定数量的字符,但是+=使用更方便

find——从前寻找字符(串)并返回下标位置

std::string::find

size_t find (const string& str, size_t pos = 0) const;	
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;
size_t find (char c, size_t pos = 0) const;

rfind——从后寻找字符(串)并返回下标位置

string::npos是一个静态无符号整型变量,大小为-1(即最大的无符号整数)

std::string::rfind

size_t rfind (const string& str, size_t pos = npos) const;	
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos, size_t n) const;	
size_t rfind (char c, size_t pos = npos) const;

rfind如果未给出查找的起始位置,或者起始位置下标越界,会从字符串末尾开始寻找;rfind和find一样,如果未找到目标,就会返回npos

substr——从字符串中截取子串

std::string::substr
string substr (size_t pos = 0, size_t len = npos) const;

如果pos等于字符串大小,就会返回空字符串;如果pos大于字符串大小,就会抛异常

#include <iostream>
#include <string>

int main ()
{
  std::string str="We think in generalities, but we live in details.";

  std::string str2 = str.substr (3,5);     // "think"

  std::size_t pos = str.find("live");      // position of "live" in str

  std::string str3 = str.substr (pos);     // get from "live" to the end

  std::cout << str2 << ' ' << str3 << '\n';

  return 0;
}

string类的非成员函数

operator+ 效率低,少用
operator+= 追加字符(串)
operator>>,operator<< 输入输出运算符重载

getline

获取一行字符串
relational operators 大小比较

operator+

std::operator+ (string)

string operator+ (const string& lhs, const string& rhs);	

string operator+ (const string& lhs, const char*   rhs);
string operator+ (const char*   lhs, const string& rhs);

string operator+ (const string& lhs, char          rhs);
string operator+ (char          lhs, const string& rhs);

这个函数是传值返回,需要深拷贝,效率低少用为好

operator+=前面提到过

operator<<operator>>

输入输出运算符重载,使用很简单,遇到空格或者换行结束

getline——获取一行字符串

std::getline (string)

istream& getline (istream& is, string& str, char delim);//读到delim字符结束

istream& getline (istream& is, string& str);//读到'\n'结束

relational operators——大小比较

relational operators (string)

bool operator== (const string& lhs, const string& rhs);
bool operator== (const char*   lhs, const string& rhs);
bool operator== (const string& lhs, const char*   rhs);

bool operator!= (const string& lhs, const string& rhs);
bool operator!= (const char*   lhs, const string& rhs);
bool operator!= (const string& lhs, const char*   rhs);

bool operator<  (const string& lhs, const string& rhs);
bool operator<  (const char*   lhs, const string& rhs);
bool operator<  (const string& lhs, const char*   rhs);

bool operator<= (const string& lhs, const string& rhs);
bool operator<= (const char*   lhs, const string& rhs);
bool operator<= (const string& lhs, const char*   rhs);

bool operator>  (const string& lhs, const string& rhs);
bool operator>  (const char*   lhs, const string& rhs);
bool operator>  (const string& lhs, const char*   rhs);
	
bool operator>= (const string& lhs, const string& rhs);
bool operator>= (const char*   lhs, const string& rhs);
bool operator>= (const string& lhs, const char*   rhs);
#include <iostream>
#include <vector>

int main ()
{
  std::string foo = "alpha";
  std::string bar = "beta";

  if (foo==bar) std::cout << "foo and bar are equal\n";
  if (foo!=bar) std::cout << "foo and bar are not equal\n";
  if (foo< bar) std::cout << "foo is less than bar\n";
  if (foo> bar) std::cout << "foo is greater than bar\n";
  if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
  if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";

  return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_74269833/article/details/132512545