string常用的成员函数

string本质是一个类,这个类由很多内置的特点,还有很多有用的成员函数。

目录

成员函数

构造函数

 析构函数 

operator=

 assign

 get_allocator

元素访问

 迭代器


成员函数

构造函数

参数

alloc用于此 string 所有内存分配的分配器
count产生的 string 大小
ch初始化 string 所用的值
pos要包含的首字符位置
first, last复制字符的来源范围
s指向用作源初始化 string 的字符数组的指针
other用作源初始化 string 的另一 string
  1. 默认构造函数。构造空 string (拥有零大小和未指定的容量)。若不提供分配器,则从默认构造的实例获得分配器。
  2. 构造拥有字符 ch 的 count 个副本的 string 。
  3. 以 other 的子串 [pos, pos+count) 构造 string 。若 count == npos [常数=-1]或未指定 count ,或若请求的子串越过字符串的结尾,则产生的子串为 [pos, other.size()) 。
  4. 以 s 所指向的字符串的首 count 个字符构造 string 。 s 能包含空字符。 string 的长度为 count 。若 [s, s + count) 不是合法范围则行为未定义。
  5. 构造拥有范围 [first, last) 内容的 string 。
  6. 以 s 所指向的空终止字符串的副本所初始化的内容构造 string 。以首个空字符确定字符串的长度。
  7. 复制构造函数。构造拥有 other 内容副本的 string 。

    代码演示:
     

    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
        //默认构造函数
        // string::string()
        string s1;
        cout << s1 << endl;//生成空字符串
    
        //count个ch字符的字符串
        //string::string(size_type count, charT ch)
        string s2(10, 'a');
        cout << s2 << endl;
    
        //以 other 的子串 [pos, pos+count) 构造 string 。
        //string::string(string const& other, size_type pos, size_type count)
        string const other("Exemplary");
        string s3(other, 0, other.length() - 1);
        cout << s3 << endl;
    
        //以 s 所指向的字符串的首 count 个字符构造 string 。
        //string::string(charT const* s, size_type count)
        string s4("I like studying.", 5);
        cout << s4 << endl;
    
        char c_str[] = "another string";
        // string::string(InputIt first, InputIt last)
        string s5(begin(c_str) + 8, end(c_str) - 1);
        cout << s5 << endl;
    
        // string::string(charT const* s)
        string s6("C++string");
        cout << s6 << endl;
    
        //string::string(const string& other)
        string const other2("Exemplar");
        string s7(other2);
        cout << s7 << endl;
    
        return 0;
    }

    执行结果

 析构函数 

用于销毁字符串 

operator=

  1.  以 str 的副本替换内容。若 *this 与 str 为同一对象,则此函数无效果。
  2. 以 s 所指向的空终止字符串的内容替换内容,如同用 assign(s, Traits::length(s)) 。
  3. 以字符 ch 的内容替换内容,如同用 assign(addressof(ch), 1) 测试代码:
    #include <iostream>
    #include <string>
    using namespace std;
    int main()
    {
    	//operator=( const CharT* )
    	string s1 = "string operator";
    	cout << "s1 = " << s1 << endl;
    
    	//operator=( const basic_string& )
    	string s2 = s1;
    	cout << "s2 = " << s2 << endl;
    
    	//operator=( CharT );
    	string s3;
    	s3 = 'C';
    	cout << "s3 = " << s3 << endl;
    
    	return 0;
    }

    运行结果:

 assign

  1. 以 count 个 ch 的副本替换内容。
  2. 以 str 的副本替换内容。等价于 *this = str;
  3. 以 str 的子串 [pos, pos+count) 替换内容。若请求的子串越过 string 尾,或若 count == npos ,则产生的子串是 [pos, str.size()) 。
  4.  以范围 [s, s+count) 中的字符的副本替换内容。
  5. 以 s 所指向的空终止字符串的内容替换内容。由首个空字符,用 Traits::length(s) 确定字符串长度。

代码测试:

#include <iostream>
using namespace std;

int main()
{
	 string s1;
	// assign(size_type count, CharT ch)
	s1.assign(10, 'a');
	cout << "s1 = " << s1 << endl;
	 
	// assign(basic_string const& str)
	string s2;
	s2.assign(s1);
	cout << "s2 = " << s2 << endl;
	
	// assign(basic_string const& str, size_type pos, size_type count)
	string s3;
	s3.assign(s1, 0, s1.length());//[0,length)
	cout << "s3 = " << s3 << endl;

	// assign(charT const* s, size_type count)
	string s4;
	s4.assign("good good study", 5);
	cout << "s4 = " << s4 << endl;

	// assign(charT const* s)
	string s5;
	s5.assign("string");
	cout << "s5 = " << s5 << endl;

	return 0;
}

 运行结果:

 get_allocator

返回与string关联的分配器,无参数。

constexpr allocator_type get_allocator() const;

元素访问

at:访问指定字符,有边界检查。

operator[]:访问指定字符

front(C++11)

back(C++11)

代码演示

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

int main()
{
	string s("This is a string.");
	//reference at( size_type pos ) ;
	cout << "at(2):" << s.at(2) << endl;
	//reference operator[]( size_type pos ) ;
	cout << "[5]:" << s[5] << endl;

	// CharT& front() ;
	//等价于 operator[](0) 
	cout << s.front() << endl;

	// CharT& back() ;
	//operator[](size() - 1) 
	cout << s.back() << endl;
}

运行结果

 

 迭代器

1. begin()/cbegin() 返回指向字符串首字符的迭代器。
begin() 返回可变或常迭代器,取决于 *this 的常性。
cbegin() 始终返回常迭代器。它等价于 const_cast<const basic_string&>(*this).begin() 。

 2.end()/end():返回指向后随字符串末字符的字符的迭代器。此字符表现为占位符,试图访问它导致未定义行为。

3.rbegin()/crbegin():返回指向逆转字符串首字符的逆向迭代器。它对应非逆向字符串的末字符。

4.rend()/crend():返回指向后随逆向字符串末字符的字符的逆向迭代器。它对应前驱非逆向字符串首字符的字符。此字符表现为占位符,试图访问它会导致未定义行为。

 代码演示:

#include <iostream>
using namespace std;

int main()
{
	string s("This is a string");
//iterator begin();
	cout << *s.begin() << endl;
//iterator end();
	cout << *(s.end() - 1) << endl;
	//end()指向末尾迭代器,减1就往前一个迭代器,不会越界访问;
	//直接访问end()迭代器会异常报错

//iterator rbegin();
	cout << *s.rbegin() << endl;
//iterator rend();
	cout << *(s.rend() - 1) << endl;
	return 0;
}

运行结果

 本篇介绍了字符串string成员函数、元素访问、迭代器最常用的几种用法。

参照网站:cppreference.com

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值