1. 程式人生 > >string裡面的常用c函式 //2018/12/18-

string裡面的常用c函式 //2018/12/18-

string裡面的函式

(1)toupper函式

該函式把小寫字母變成大寫字母;
例項:

#include<iostream>  
#include<string>  
#include<cctype>  
using namespace std;  
  
int main()  
{
	
	char c = 'q';
	c = toupper(c);
	cout<<c;
}
//這個函式將cout<<Q;

(2)substr函式

用途:一種構造string的方法
形式:s.substr(pos, n)
解釋:返回一個string,包含s中從pos開始的n個字元的拷貝(pos的預設值是0,n的預設值是s.size() - pos,即不加引數會預設拷貝整個s)
補充:若pos的值超過了string的大小,則substr函式會丟擲一個out_of_range異常;若pos+n的值超過了string的大小,則substr會調整n的值,只拷貝到string的末尾
例項:

#include<string>
#include<iostream>
using namespace std;
int main()
{
  string s("12345asdf");
  string a = s.substr(0,5);     //獲得字串s中從第0位開始的長度為5的字串
  cout << a << endl;
}

(3)erase函式

erase函式的原型如下:
(1)string& erase ( size_t pos = 0, size_t n = npos );
(2)iterator erase ( iterator position );
(3)iterator erase ( iterator first, iterator last );
也就是說有三種用法:
(1)erase(pos,n); 刪除從pos開始的n個字元,比如erase(0,1)就是刪除第一個字元
(2)erase(position);刪除position處的一個字元(position是個string型別的迭代器)
(3)erase(first,last);刪除從first到last之間的字元(first和last都是迭代器)
下面是例子:

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

int main ()
{
  string str ("This is an example phrase.");
  string::iterator it;

  // 第(1)種用法
  str.erase (10,8);
  cout << str << endl;        // "This is an phrase."

  // 第(2)種用法
  it=str.begin()+9;
  str.erase (it);
  cout << str << endl;        // "This is a phrase."

  // 第(3)種用法
  str.erase (str.begin()+5, str.end()-7);
  cout << str << endl;        // "This phrase."
  return 0;
}

(4)邏輯錯誤

int main()
{	
	string i="asd";
	i[2]='\0';
	i=i+'p';
	cout<<i;//會輸出“asd p”,而不是輸出“asdp” 
}

(5)C++刪除string最後一個字元的幾種方法

#include<iostream>
#include<string>
using namespace std;
int main() 
{
	string str;
	str = "123456";
	cout << str << endl;
 
	//方法一:使用substr()
	str = str.substr(0, str.length() - 1);
	cout << str << endl;
 
	//方法二:使用erase()
	str.erase(str.end() - 1);
	cout << str << endl;
 
	//方法三:使用pop_back()
	str.pop_back();
	cout << str << endl;
	return 0;
}

(6)to_string函式

函式原型:
string to_string (int val);
string to_string (long val);
string to_string (long long val);
string to_string (unsigned val);
string to_string (unsigned long val);
string to_string (unsigned long long val);
string to_string (float val);
string to_string (double val);
string to_string (long double val);

功能:

將數值轉化為字串。返回對應的字串。

示例:

#include <iostream>   
#include <string>     
using namespace std;
 
int main ()
{
  string pi = "pi is " + std::to_string(3.1415926);
  string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
  cout << pi << '\n';
  cout << perfect << '\n';
  return 0;
}

結果:
pi is 3.141593
28 is a perfect number

(7)insert()函式


#include <iostream>
#include <string>
 
int main (){
  std::string str="to be question";
  std::string str2="the ";
  std::string str3="or not to be";
  std::string::iterator it;
  // used in the same order as described above:
  str.insert(6,str2);                 // to be (the )question
  str.insert(6,str3,3,4);             // to be (not )the question
  str.insert(10,"that is cool",8);    // to be not (that is )the question
  str.insert(10,"to be ");            // to be not (to be )that is the question
  str.insert(15,1,':');               //加一個'.' to be not to be(:) that is the question
  it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
  str.insert (str.end(),3,'.');       // to be, not to be: that is the question(...)
  str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
  std::cout << str << '\n';
  return 0;
}
//output  to be, or not to be: that is the question...

string的成員函式insert有以下多種過載:

string &insert(int p0, const char *s);——在p0位置插入字串s

string &insert(int p0, const char *s, int n);——在p0位置插入字串s的前n個字元

string &insert(int p0,const string &s);——在p0位置插入字串s

string &insert(int p0,const string &s, int pos, int n);——在p0位置插入字串s從pos開始的連續n個字元

string &insert(int p0, int n, char c);//在p0處插入n個字元c

iterator insert(iterator it, char c);//在it處插入字元c,返回插入後迭代器的位置

void insert(iterator it, const_iterator first, const_iteratorlast);//在it處插入從first開始至last-1的所有字元

void insert(iterator it, int n, char c);//在it處插入n個字元c