1. 程式人生 > >C++中string型別insert方法用法集錦

C++中string型別insert方法用法集錦

C++方法的用法真的太多了,一個insert方法用法就多達8種,一不留神就用錯來了,很神傷微笑

// inserting into a string
#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;
}

相關推薦

C++string型別insert方法用法集錦

C++方法的用法真的太多了,一個insert方法用法就多達8種,一不留神就用錯來了,很神傷。// inserting into a string #include <iostream> #i

C++string型別求長度用法以及c_str用法總結

1.C++中求string型別的長度有三種方式。在c++中,string代表一個類,有它自己的建構函式和成員函式。有兩個成員函式都可以求string型別的長度。①  length()成員函式。②  size()成員函式。③  可以藉助strlen函式,但是前提是需要將stri

C#string型別的常用方法總結

在C#語言中,字串是System.String類的一個引用型別,但與其他引用型別不同的是,C#將字串視為一個基本型別,它可以申請為一個常量,也可以直接給它賦值。由於C#中的字串是由System.String類派生而來的引用物件,因此可以使用String類的方法來

C++push_back,push_front,insert用法

之前在介紹vector的初始化的時候有介紹到容器的push_back操作,push_back基本所有容器都支援,而push_front則是有的支援有的不支援。insert和push_back一樣都支援。

C++string常用函式及用法總結

標準c++中string類函式介紹 注意不是CString 之所以拋棄char*的字串而選用C++標準程式庫中的string類,是因為他和前者比較起來,不必 擔心記憶體是否足夠、字串長度等等,而且作為一個類出現,他整合的操作函式足以完成我們大多數情況下(甚至是1

C#String的常用方法

[特點] 1.可以看作是一個只讀的char陣列 2.不可變性,即字串中的字元內容不能改,,當重新給改變數賦值時,知識變數的指向改變了,但可以讀。如果我      想”改變“字串中字元的內容,怎麼辦那?

C++ string.find() 函式的用法總結

 #include <string> #include <iostream> using namespace std; void main() { ////find函式返回型別 size_typestring s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8

C++string型別與char*型別的字串比較剖析

C++中的string字串可以直接進行比較,事實上strcmp()的兩個引數是char*型別的,也就是說是比較C字串的(即char陣列),於是就不能用於比較string型別了 int strcmp( const char *string1, const char *str

C++ string.find() 函式的用法總結(轉載)

 #include <string>#include <iostream>using namespace std; void main() { ////find函式返回型別 size_type string s("1a2b3c4d5e6f7g8

C++路徑的處理方法string

ble fin ase names 復制字符串 ostream 是否 substr() 新的 string 類提供字符串處理函數,利用這些函數,程序員可以在字符串內查找字符,提取連續字符序列(稱為子串),以及在字符串中刪除和添加。我們將介紹一些主要函數。 1.函數find_

C#string.format用法詳解

個數 date 其中 位置 tr1 bsp 位數 數值 日期格式化 tring.Format 方法的幾種定義: String.Format (String, Object) 將指定的 String 中的格式項替換為指定的 Object 實例的值的文本等效項。String.F

C++int型別String型別的相互轉換

最近經常用到兩種型別的相互轉換,從網上找了一些,彙總一下,以備不時之需 int型別轉換為String型別 方法一:利用sprintf #include <iostream> #include <string> int main() { int n =

jsString型別的常用方法

js中String型別的常用方法 var str1 = new String(“hello”); var str2 = new String(“hello”); document.write(“兩個字串的物件一樣嗎?”+(str1.toString()==str2.toString(

C++string用法

gin 替換 手寫 是否 如何 定義 ins app 比較 我們知道string可以構造一個字符串變量,那麽它的操作有哪些呢。它包含在string庫中(不是string.h沒有.h),它可以和定義一個字符一樣定義一個字符串變量,而且強大的C++還內置了各種函數,基本實現不用

C# String.Join()方法

  今天在工作中看到了組裡一個大佬寫的程式碼,感觸頗多,同樣實現一個需求,我寫迴圈費了老大勁,程式碼又臭又長,大佬的程式碼簡潔明瞭,三行搞定。。。不得不說,今天賺大了   簡單總結一下今天賺到的知識   string裡邊的Join()方法    &nb

VS程式設計,C#string字串過長,回車換行寫在不同的行的一種方法

1、使用@對字串進行轉義。 “”雙引號裡裡面的特殊字元不再具有轉義功能,例如\n不再被轉義成換行符。 2、使用@對字串進行轉義,若字串中包含雙引號,則需要在雙引號外,再加一個雙引號以區分。 例如: string sqlString =

C++string substr函式的使用方法

substr函式的形式為s.substr(pos, n), 需要兩個引數,第一個是開始位置,第二個是獲取子串的長度。 函式可以從一個字串中獲取子串,返回一個string,包含s中從pos開始的n個字元的拷貝(pos的預設值是0,n的預設值是s.size() -

C++char型別string區別與聯絡

C++中內建了一些基本型別,包含算數型別和空型別。 算數型別(arithmetic type)包含字元、整數型、布林型和浮點數。其中基本的字元型別是char型別,最小尺寸為8位位元組。 C++除了內建型別外,還有抽象資料型別庫,而string則是其中重要的一個標準庫型別。S

C++string和vector用法總結

string 包含標頭檔案:#include<string> 申明名稱空間:using std::string ; 1)       初始化 string s1;   //Default initialization; s1 is an empty string