C++のstring常用関数集錦

7940 ワード

私たちはテーマに直行します.次は私が今日説明する関数のリストです.
/*
1.  find  
2.  insert  
3.  substr
4.  erase
5.  replace
*/

最初に、関数を検索します.
/*
   	                                
find	                 
rfind	                   
find_first_of	                   ,       
find_first_not_of                   ,       
find_last_of	                   ,        
find_last_not_of                    ,        
*/
以上の関数はすべて4回リロードされ、以下はfind_first_of関数を例に、彼らのパラメータを説明します.他の関数はそのパラメータと同じです.つまり、全部で24の関数があります.
/*
size_type find_first_of(const basic_string& s, size_type pos = 0)
size_type find_first_of(const charT* s, size_type pos, size_type n)
size_type find_first_of(const charT* s, size_type pos = 0)
size_type find_first_of(charT c, size_type pos = 0) 
*/
すべての検索関数はsizeを返します.typeタイプ.この戻り値は、通常、見つかった文字列の位置であり、見つからない場合はstring::nposを返します.string::nposとの比較はstring::size_を使用する必要があります.typeはintやunsigned intなどのタイプを直接使用しないで使用します.実はstring::nposは-1を表しています.ヘッダーファイルを見てください.
template <class _CharT, class _Traits, class _Alloc>
const basic_string<_CharT, _Traits, _Alloc>::size_type
basic_string<_CharT, _Traits, _Alloc>::npos
= basic_string<_CharT, _Traits, _Alloc>::size_type) - 1;
ここではfindの使用例を後で示しますが、一番後ろの4つを見てみましょう.
行の先頭と末尾のすべての非英語文字をフィルタする必要があります.stringでどのように実現するかを見てみましょう.
#include <string>  
#include <iostream>  
using namespace std;
int main() {
	string strinfo = "   //*---Hello Word!......------";
	string strset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	int first = strinfo.find_first_of(strset);
	if (first == string::npos) {
		cout << "not find any characters" << endl;
		return -1;
	}
	int last = strinfo.find_last_of(strset);
	if (last == string::npos) {
		cout << "not find any characters" << endl;
		return -1;
	}
	cout << strinfo.substr(first, last - first + 1) << endl;
	return 0;
}

/*
    :Hello Word
*/
ここでは、すべての英字大文字と小文字を検索する文字セットとして、最初の英字の位置を検索し、最後の英字の位置を検索し、substrで中間の一部を出力します.しかし、個人的にはこの関数が届く場所も数カ所だと思います.例えば、HelloとWordの間にはいくつかの文字があるので、自分で関数を書いて探すしかありません.だから具体的な問題は具体的に対処して、問題を考える時よく考えて、この関数はあなたに提供して、あなたにもっと便利な操作コードを提供しますが、しかし考えなければならないのは全面的で、関数の機能はあなたが直面している問題と一致していますか.あるいはこの関数はあなたのどれだけの問題を解決することができて、他のもっと効率的なあるいはもっと便利な方法がありますか?
では、find関数を見てみましょう.find関数は順方向一致、つまり最初の一致する文字や文字列を見つけます.次は2つの方向から言えば、stringの中でstringとcharを探します.
#include<iostream>
#include<string>
#include<algorithm>//find    
using namespace std;
int main()
{
	string str1 = "123456";
	//    string  string
	string str2 = "34";//    
	string str3 = "364";//    

	int a = str1.find(str2);
	int b = str1.find(str3);
	cout << a << endl;
	cout << b << endl;
	/*
	   :
	         2
	        -1
	*/


	//    string  char
	char ch1 = '3';//    
	char ch2 = '9';//    

	int aa = str1.find(ch1);
	int bb = str1.find(ch2);

	cout << aa << endl;
	cout << bb << endl;
	/*
	   :
	       2
	       -1
	*/
	return 0;
}

ここで言うかもしれませんが、文字配列をあげたら?例えば、char a[10]=「123456789」からchar ch='5」またはchar[5]=「4567」を検索しますか?
http://www.cplusplus.com/reference/cstring/strstr/?kw=strstrこれはCプラスプラスプラスプラスプラスの説明です
rfind関数を見てみましょう.この関数は逆方向に一致していることに注意してください.つまり、最後の一致を見つけます.
#include<iostream>
#include<string.h>
#include<string>
using namespace std;
int main()
{
	string str1="456456";

	string str2="456";
	string str3="654";

	int a=str1.rfind(str2);
	int b=str1.rfind(str3);

	cout<<a<<endl;//  3
	cout<<b<<endl;//  -1
	return 0;
}
2つ目:関数の挿入
string& insert (size_t pos, const string& str);
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen = npos);
string& insert (size_t pos, const char* s);
string& insert (size_t pos, const char* s, size_t n);	
string& insert (size_t pos,   size_t n, char c);
これはよく使われるリロードです.
#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  6     str2
  str.insert(6,str3,3,4);             // to be (not )the question str  6     str3     ,    3     4   
  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,3,':');               // to be not to be(:::) that is the question
  std::cout << str << '
'; return 0; }

3つ目:抽出
string substr (size_t pos = 0, size_t len = npos) const;

比較的に簡単で、直接C plus plusのあげる例を見ます
// string::substr
#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 << '
';// :think live in details. return 0; }
4番目:関数の削除
string& erase (size_t pos = 0, size_t len = npos)
iterator erase (iterator p)	
iterator erase (iterator first, iterator last)
やはりCプラスプラスプラスプラスプラスの例を見てみましょう
// string::erase
#include <iostream>
#include <string>
int main()
{
	std::string str("This is an example sentence.");
	std::cout << str << '
'; // "This is an example sentence." str.erase(10, 8); std::cout << str << '
'; // "This is an sentence." std::cout << str << '
'; // "This is a sentence." str.erase(str.begin() + 5, str.end() - 9); std::cout << str << '
'; // "This sentence." return 0; }

5つ目:replaceの置換
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>//   
using namespace std;
int main()
{
	/*    :     ,       
	1.template <class ForwardIterator, class T>
	void replace (ForwardIterator first, ForwardIterator last,const T& old_value, const T& new_value);
	2.basic_string& replace(size_type pos1,size_type n1,const basic_string& str);
	*/
	string str = "take a right turn";
	str.replace(7, 5, "left");//    &str[7] &str[12]      left
	cout << str << endl;//take a left turn

	//  ,     find()     replace()      
	string s1 = "old";
	string s2 = "mature";
	string s3 = "the old man and the sea";
	int pos = s3.find(s1);
	if (pos != string::npos)
		s3.replace(pos, s1.size(), s2);
	//     old  mature

	//      
	int myints[] = { 10, 20, 30, 30, 20, 10, 10, 20 };
	vector<int> myvector(myints, myints + 8);            // 10 20 30 30 20 10 10 20
	replace(myvector.begin(), myvector.end(), 20, 99);   // 10 99 30 30 99 10 10 99
	//myvector   20  99
	cout << "myvector contains:";
	for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
		cout << ' ' << *it;
	cout << '
'; /* : myvector contains: 10 99 30 30 99 10 10 99 */ return 0; }