string常用的操作

目录

容量

增加操作

insert

push_back

append

operator+=

查找操作

删除操作

clear

erase

pop_back(C++11)

更改操作

其他操作


容量

empty:检查 string 是否无字符,即是否 begin() == end() 

size/length:返回 string 中的 CharT 元素数,即 std::distance(begin(), end()) 。

reserve:保留存储

capacity:返回当前对象分配的存储空间能保存的字符数量

代码演示:

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s;

	//bool empty() const;
	if (s.empty()) {
		cout << "s is empty" << endl;
	}//s is empty
	
	s = "C++";
	//size_type size() const;
	cout << s.size() << endl;//3
	//size_type length() const;
	cout << s.length() << endl;//3

	//size_type capacity() const;
	cout << s.capacity() << endl;//15
	//void reserve( size_type new_cap = 0 );
	s.reserve(100);
	cout << s.capacity() << endl;//111
	return 0;
}

运行结果

增加操作

insert

  1. 在位置 index 插入 count 个字符 ch 的副本。
  2. 在位置 index 插入 s 所指向的空终止字符串。字符串的长度由首个空字符,通过 Traits::length(s) 确定。
  3. 在位置 index 插入字符串 str
  4. 在 pos 所指向的字符前插入字符 ch
  5.  在 pos 所指向的元素(如果存在)前插入 count 个字符 ch 的副本。

代码演示

#include <iostream>
using namespace std;

int main()
{
	string s; 
	// insert(size_type index, size_type count, char ch)
	s.insert(0, 2, '+');
	cout << "s1:" << s << endl;

	// insert(size_type index, const char* s)
	s.insert(0, "C");
	cout << "s1:" << s << endl;

	// insert(size_type index, string const& str)
	string s2 = "~";
	s2.insert(0, s);
	cout << "s2:" << endl;

	// insert(const_iterator pos, char ch)
	s2.insert(s2.begin(), '>');
	cout << "s2:" << s2 << endl;

	// insert(const_iterator pos, size_type count, char ch)
	s2.insert(s2.begin() + s2.find_first_of('~') + 1, 2, '>');
	cout << "s2:" << s2 << endl;

	return 0;
}

 运行结果:

push_back

后附给顶字符ch到字符串尾

constexpr void push_back( CharT ch );

代码演示

append

  1. 后附 count 个 ch 的副本
  2. 后附 string str
  3. 后附 str 的子串 [pos, pos+count) 。若请求的子串越过 string 结尾,或若 count == npos ,则后附的子串为 [pos, size()) 。。
  4. 后附 s 所指向的空终止字符串。由首个空字符用 Traits::length(s) 确定字符串的长度
  5. 后附范围 [s, s + count) 中的字符。此范围能含有空字符

代码演示

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s1 = " string ";
	string str;

	//string& append(size_type count,CharT ch);
	str.append(3, '*');
	cout << str << endl;

	//string& append( const basic_string& str )
	str.append(s1);
	cout << str << endl;

	//string& append( const basic_string& str,
	//					size_type pos, size_type count );
	str.append(s1, 1, 3);
	cout << str << endl;
	
	//string& append( const CharT* s );
	const char* s = " is wrong";
	str.append(s);
	cout << str << endl;

	//string& append( const CharT* s, size_type count );
	const char* c = "####";
	str.append(c, 2);
	cout << str << endl;

}

 运行结果:

operator+=

  1. 后附 string str 。
  2. 后附字符 ch 。
  3.  后附 s 所指向的空终止字符串。
#include <iostream>
using namespace std;

int main()
{
	string s1 = "I ";
	string s2 = " am ";
	const char* c = "Jack";
	//string& operator+=( const basic_string& str );
	s1 += s2;
	cout << s1 << endl;

	// string& operator+=( CharT ch );
	s1 += c;
	cout << s1 << endl;

	//string& operator+=( const CharT* s );
	s1 += '!';
	cout << s1 << endl;

	return 0;
}

运行结果:

 

查找操作

find

寻找首个等于给定字符序列的子串。搜索始于 pos ,即找到的子串必须不始于 pos 之前的位置。

  1. 寻找等于 str 的首个子串。
  2.  寻找等于范围 [s, s+count) 的首个子串。此范围能含空字符。
  3. 寻找等于 s 所指向的字符串的首个子串。由首个空字符,用 Traits::length(s) 确定字符串长度。
  4. 寻找首个字符 ch (由后述规则当作单字节子串)。

代码演示

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string const s = "This is a string";

	//size_type find(const basic_string & str,
	//			size_type pos = 0) const;
	string s2 = "str";
	cout << s.find(s2) << endl;

	//size_type find( const CharT* s, size_type pos, size_type count ) const;
	int n = s.find("is");
	cout << n << endl;
	n = s.find("is", 5);
	cout << n << endl;

	//size_type find(CharT ch, size_type pos = 0) const;
	n = s.find('a');
	cout << "'a'=" << n << endl;

	n = s.find('p');
	cout << "'p'=" << n << endl;

	return 0;
}

运行结果 

 rfind:反向搜索字符串,操作和上述的find函数类似。

删除操作

clear

清除内容
如同通过执行 erase(begin(), end()) 从 string 移除所有字符。

constexpr void clear() noexcept;

代码演示: 

#include <iostream>
#include <string>
using namespace std;

int main()
{
	string s{ "String" };
	cout << "before:" << s << endl;
	s.clear();
	cout << "after:" << s << endl;
	cout << s.capacity() << endl;
	cout << s.size() << endl;
	return 0;
}

 运行结果:

 

erase

从字符串移除指定的字符

  1. 移除从 index 开始的 std::min(count, size() - index) 个字符。
  2.  移除位于 position 的字符。
  3. 移除范围 [first, last) 中的字符。

 代码演示

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string s1 = "This is a string.";
	cout << "1:" << s1 << endl;

	//string& erase( size_type index = 0, size_type count = npos );
	s1.erase(7, 2);
	cout << "1:" << s1 << endl;

	//iterator erase( const_iterator position );
	//find(s1.begin(), s1.end(), ' ')返回查找字符的迭代器
	s1.erase(find(s1.begin(), s1.end(), ' '));
	cout << "2:" << s1 << endl;

	//iterator erase( const_iterator first, const_iterator last );
	s1.erase(s1.begin(), s1.end());
	cout << "3:" << s1 << endl;
	return 0;
}

运行结果:

 

pop_back(C++11)

从字符串中移除末字符,等价于 erase(end() - 1, 1) 。若字符串为空则行为未定义。

constexpr void pop_back();

更改操作

replace

以新字符串替换 [pos, pos + count) 或 [first, last) 所指示的 string 部分

resize

重设 string 大小以含 count 个字符。

若当前大小小于 count ,则后附额外的字符。

若当前大小大于 count ,则缩减 string 到为其首 count 个元素。

swap

交换 string 与 other 的内容。可能非法化所有迭代器和引用。

代码演示

#include <iostream>
using namespace std;

int main()
{
	string s = "This are a string";
	//string& replace(size_type pos, size_type count,
	//					const  string & str);
	s.replace(5, 3, "is");
	cout << s << endl;
	string s1 = "That";

	//string& replace( const_iterator first, const_iterator last,
	//                  const   string& str );
	s.replace(s.begin(), s.begin()+4,s1);
	cout << s << endl;

	//void resize( size_type count, CharT ch );
	s.resize(10, 'a');
	cout << s << endl;
	s.resize(20, 'a');
	cout << s << endl;

	cout << "s:" << s << endl;
	cout << "s1:" << s1 << endl;
	//void swap(string & other);
	s.swap(s1);
	cout << "s:" << s << endl;
	cout << "s1:" << s1 << endl;
	return 0;
}

运行结果

 

其他操作

compare

比较两个字符序列。

返回值:

  • *this 在字典序中先出现于参数所指定的字符序列时是负值。
  • 两个序列比较等价时为零。
  • *this 在字典序中后出现于参数所指定的字符序列时是正值。

substr()

返回子串 [pos, pos+count) 。若请求的字串越过字符串的结尾,即 count 大于 size() - pos

则返回的子串为 [pos, size()]。

copy

复制子串 [pos, pos+count) 到 dest 所指向的字符串。若请求的子串越过 string 结尾,或若 count == npos ,则复制的子串为 [pos, size()) 。产生的字符串不是空终止的。

代码演示

#include <iostream>
using namespace std;

int main()
{
	string s = "This is a cat";

	string s1 = "a cat";
	string s2 = "a cat";
	//int compare( const basic_string& str ) const;
	cout << "s - s1:" << s.compare(s1) << endl;
	cout << "s1 - s2:" << s1.compare(s2) << endl;

	//int compare( size_type pos1, size_type count1,
	//             const basic_string& str ) const;
	cout << s.compare(s.find('a'), 5, s1) << endl;

	//string substr( size_type pos = 0, size_type count = npos ) const;
	cout << s.substr(0, 5) << endl;

	//size_type copy( CharT* dest, size_type count, size_type pos = 0 ) const;
	char c[15];
	int n = s.copy(c, sizeof(c), 0);
	cout << n << endl;
	c[n] = '\0';
	cout << "s:" << s << endl;
	cout << "c:" << c << endl;
}

运行结果:

 本篇概述了字符串string最常用的几种操作,增删查改等。

参考网站:cppreference.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值