【C++grammar】string類和array類

目錄

  • 1、C++11的string類
    • 1、創建 string 對象
    • 2、追加字符串append函數
    • 3、為字符串賦值assign函數
    • 4、at, clear, erase, and empty函數
    • 5、比較字符串compare()
    • 6、獲取子串at() 、substr()函數
    • 7、搜索字符串find()
    • 8、插入和替換字符串insert() 、replace()
    • 9、字符串運算符
    • 10、string_view與string的幾點差別
  • 2、C++11的數組類
    • 1. C-Style Array v.s. C++ Style Array (C風格數組和C++風格數組)
      • 1、C Style Array (C++ raw array,也叫做C++原生數組)
      • 2、C Style Array (C++ raw array,也叫做C++原生數組)
    • 2. Create C++ Style Array (創建C++風格數組)
    • 3.std::array的成員函數

C++ 使用 string 類處理字符串

string類中的函數

(1) 構造

(2) 追加

(3) 賦值

(4) 位置與清除

(5) 長度與容量

(6) 比較

(7) 子 串

(8) 搜索

(9) 運算符

1、C++11的string類

1、創建 string 對象

由一個字符串常量或字符串數組創建string對象

string message{ "Aloha World!" };
char charArray[] = {'H', 'e', 'l', 'l', 'o', '\0'};
string message1{ charArray };

2、追加字符串append函數

一系列的重載函數可以將新內容附加到一個字符串中

string s1{ "Welcome" };
s1.append( " to C++" ); // appends " to C++" to s1
cout << s1 << endl; // s1 now becomes Welcome to C++string s2{ "Welcome" };
s2.append( " to C and C++", 3, 2 ); // appends " C" to s2
cout << s2 << endl; // s2 now becomes Welcome Cstring s3{ "Welcome" };
s3.append( " to C and C++", 5); // appends " to C" to s3
cout << s3 << endl; // s3 now becomes Welcome to Cstring s4{ "Welcome" }; 
s4.append( 4, 'G' ); // appends "GGGG" to s4
cout << s4 << endl; // s4 now becomes WelcomeGGGG

3、為字符串賦值assign函數

一系列的重載函數可以將一個字符串賦以新內容

string s1{ "Welcome" };
s1.assign( "Dallas" ); // assigns "Dallas" to s1
cout << s1 << endl; // s1 now becomes Dallasstring s2{ "Welcome" };
s2.assign( "Dallas, Texas", 1, 3 ); // assigns "all" to s2
cout << s2 << endl; // s2 now becomes allstring s3{ "Welcome" };
s3.assign( "Dallas, Texas", 6 ); // assigns "Dallas" to s3
cout << s3 << endl; // s3 now becomes Dallasstring s4{ "Welcome" };
s4.assign( 4, 'G' ); // assigns "GGGG" to s4
cout << s4 << endl; // s4 now becomes GGGG

4、at, clear, erase, and empty函數

(1) at(index): 返回當前字符串中index位置的字符
(2) clear(): 清空字符串
(3) erase(index, n): 刪除字符串從index開始的n個字符
(4) empty(): 檢測字符串是否為空

string s1{ "Welcome" };cout << s1.at(3) << endl; // s1.at(3) returns ccout << s1.erase(2, 3) << endl; // s1 is now Wemes1.clear(); // s1 is now emptycout << s1.empty() << endl; // s1.empty returns 1 (means true)

5、比較字符串compare()

compare() 函數用于比較兩個字符串。它與C語言中的 strcmp() 函數很像。

string s1{ "Welcome" };
string s2{ "Welcomg" };
cout << s1.compare(s2) << endl; // returns -2
cout << s2.compare(s1) << endl; // returns 2
cout << s1.compare("Welcome") << endl; // returns 0

6、獲取子串at() 、substr()函數

string s1{ "Welcome" };
cout << s1.substr(0, 1) << endl; // returns W;  從0號位置開始的1個字符
cout << s1.substr(3) << endl; // returns come;  從3號位置直到末尾的子串
cout << s1.substr(3, 3) << endl; // returns com;從3號位置開始的3個字符

7、搜索字符串find()

find() 函數可以在一個字符串中搜索一個子串或者一個字符

string s1{ "Welcome to C++" };
cout << s1.find("co") << endl; // returns 3; 返回子串出現的第一個位置
cout << s1.find("co", 6) << endl; // returns -1 從6號位置開始查找子串出現的第一個位置
cout << s1.find('o') << endl; // returns 4    返回字符出現的第一個位置
cout << s1.find('o', 6) << endl; // returns 9   從6號位置開始查找字符出現的第一個位置

8、插入和替換字符串insert() 、replace()

string s1("Welcome to C++");
s1.insert(11, "Java and ");
cout << s1 << endl; // s1 becomes Welcome to Java and C++string s2{ "AA" };
s2.insert(1, 4, 'B'); //在1號位置處連續插入4個相同字符
cout << s2 << endl; // s2 becomes to ABBBBAstring s3{ "Welcome to Java" };
s3.replace(11, 4, "C++"); //從11號位置開始向后的4個字符替換掉。注意'\0'
cout << s3 << endl; // returns Welcome to C++ 

9、字符串運算符

在這里插入圖片描述

string s1 = "ABC"; // The = operatorstring s2 = s1;    // The = operatorfor (int i = s2.size() - 1; i >= 0; i--)cout << s2[i]; // The [] operatorstring s3 = s1 + "DEFG"; // The + operatorcout << s3 << endl; // s3 becomes ABCDEFGs1 += "ABC";cout << s1 << endl; // s1 becomes ABCABCs1 = "ABC";s2 = "ABE";cout << (s1 == s2) << endl; // Displays 0 cout << (s1 != s2) << endl; // Displays 1 cout << (s1 >  s2) << endl; // Displays 0 cout << (s1 >= s2) << endl; // Displays 0 cout << (s1 <  s2) << endl; // Displays 1 cout << (s1 <= s2) << endl; // Displays 1 

10、string_view與string的幾點差別

C++中的string類與Java中的String類不同。為了有一個和Java中的String類特性類似的東西,C++17中提供了string_view類。請你查查資料,說說string_view與string的幾點差別。
C++的string對象,如果大于默認的字符串長度閥值。對于長度為N的字符串,時間成本為O(n),空間成本是2xS(n);

于是C++17就有了string_view這個標準庫的擴展,這個擴展極大地解決了string拷貝的空間成本和時間成本問題。

string_view基本沒有涉及內存的額外分配。

string_view 是C++17所提供的用于處理只讀字符串的輕量對象。這里后綴 view 的意思是只讀的視圖。

通過調用 string_view 構造器可將字符串轉換為 string_view 對象。string 可隱式轉換為 string_view。string_view 是只讀的輕量對象,它對所指向的字符串沒有所有權。string_view通常用于函數參數類型,可用來取代 const char* 和 const string&。string_view 代替 const string&,可以避免不必要的內存分配。string_view的成員函數即對外接口與 string 相類似,但只包含讀取字符串內容的部分。string_view::substr()的返回值類型是string_view,不產生新的字符串,不會進行內存分配。string::substr()的返回值類型是string,產生新的字符串,會進行內存分配。string_view字面量的后綴是 sv。(string字面量的后綴是 s)

string_view的適用場合
(由于string_view對象無法被使用它的函數修改,因此要更新string_view所引用的字符串副本,還是需要修改它所引用的string類型的內部字符串副本。)

字符串查找
遍歷字符串
顯示字符串

2、C++11的數組類

1. C-Style Array v.s. C++ Style Array (C風格數組和C++風格數組)

1、C Style Array (C++ raw array,也叫做C++原生數組)

特點:

int arr[ ] = { 1, 2, 3 };
arr 可能會退化為指針:void f(int a[]) { std::cout << sizeof(a)/sizeof(a[0]); }
arr 不知道自己的大小: sizeof(arr)/sizeof(arr[0])
兩個數組之間無法直接賦值: array1 = array2;
不能自動推導類型:auto a1[] = {1,2,3};

2、C Style Array (C++ raw array,也叫做C++原生數組)

特點;

是一個容器類,所以有迭代器(可以認為是一種用于訪問成員的高級指針)
可直接賦值
知道自己大小:size()
能和另一個數組交換內容:swap()
能以指定值填充自己: fill()
取某個位置的元素( 做越界檢查) :at()

2. Create C++ Style Array (創建C++風格數組)

#include
std::array< 數組 類型, 數組大小> 數組名字;
std::array< 數組 類型, 數組大小> 數組 名字 { 值1, 值2, …};

限制與C風格數組相同 std::array<int , 10> x;
std::array<char , 5> c{ ‘H’,‘e’,‘l’,‘l’,‘o’ };

3.std::array的成員函數

在這里插入圖片描述
在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377900.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377900.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377900.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

六、聚類算法實戰

所有代碼塊都是在Jupyter Notebook下進行調試運行&#xff0c;前后之間都相互關聯。 文中所有代碼塊所涉及到的函數里面的詳細參數均可通過scikit-learn官網API文檔進行查閱&#xff0c;這里我只寫下每行代碼所實現的功能&#xff0c;參數的調整讀者可以多進行試驗調試。多動手…

超圖軟件試用許可操作步驟_軟件中的操作步驟

超圖軟件試用許可操作步驟The software comprises of three things: Program code, Documentation, and the Operating Procedures. The Program code is the entire software code. The Documentation is produced while the development of the software itself for the time…

mysql 2013錯誤

參考資料&#xff1a; 自由呼吸的世界-mysql 2013錯誤解決 windows下mysql日志文件開啟 今天&#xff0c;莫名其妙的來了個mysql 2013錯誤&#xff0c;導致無法登陸mysql gui工具&#xff0c;而且dos也進不去&#xff0c;提示ping 127.0.0.1,百度google后&#xff1a; 這是在使…

【嵌入式系統】STM32配置FreeRTOS以及利用多線程完成流水燈、按鍵、蜂鳴器、數碼管工作

目錄1、利用STM32CubeMX配置FreeRTOS2、完成流水燈、按鍵、蜂鳴器數碼管工作1、在gpio.c和.h文件里面書寫并聲明按鍵掃描和led、數碼管子程序2、在freertos.c文件里面設置全局變量并且在各自任務中載入程序3、關于FreeRTOS的注意事項1、利用STM32CubeMX配置FreeRTOS 假設我們之…

學好Java開發的關鍵七步

在學習編程的過程中&#xff0c;我覺得不止要獲得課本的知識&#xff0c;更多的是通過學習技術知識提高解決問題的能力&#xff0c;這樣我們才能走在最前方&#xff0c;本文主要講述如何學好Java開發的關鍵七步&#xff0c;更多Java專業知識&#xff0c;廣州瘋狂Java培訓為你講…

css模糊_如何使用CSS模糊圖像?

css模糊Introduction: 介紹&#xff1a; Sometimes even the professional developers tend to forget the various basic properties which can be applied to solve very simple problems, therefore the fundamentals of developing a website or web page should be very …

七、決策樹算法和集成算法

一、決策樹算法 Ⅰ&#xff0c;樹模型 決策樹&#xff1a;從根節點開始一步步走到葉子節點&#xff08;決策&#xff09; 所有的數據最終都會落到葉子節點&#xff0c;既可以做分類也可以做回歸 對于分類&#xff1a;是由眾數決定的&#xff0c;例如爺爺奶奶媽媽都是負數&…

leetcode 538. 把二叉搜索樹轉換為累加樹 思考分析

題目 給出二叉 搜索 樹的根節點&#xff0c;該樹的節點值各不相同&#xff0c;請你將其轉換為累加樹&#xff08;Greater Sum Tree&#xff09;&#xff0c;使每個節點 node 的新值等于原樹中大于或等于 node.val 的值之和。 提醒一下&#xff0c;二叉搜索樹滿足下列約束條件&…

SQL中GROUP BY語句與HAVING語句的使用

最近在學習SQL Server相關知識&#xff0c;一直不知道怎么使用GROUP BY語句&#xff0c;經過研究和練習&#xff0c;終于明白如何使用了&#xff0c;在此記錄一下同時添加了一個自己舉的小例子&#xff0c;通過寫這篇文章來加深下自己學習的效果&#xff0c;還能和大家分享下&a…

scala語言示例_var關鍵字與Scala中的示例

scala語言示例Scala var關鍵字 (Scala var keyword) The var Keyword in scala is used to declare variables. As Scala does not require explicit declaration of data type of the variable, var keyword is used. The variable declared using the var keyword as mutable…

八、決策樹算法實驗可視化展示

一、樹模型的可視化展示 官網下載安裝包 右擊管理員身份運行&#xff0c;直接下一步即可。 配置環境變量&#xff1a; 將安裝好的可視化軟件的bin文件夾路徑添加到系統環境變量Path下即可 打開cmd&#xff0c;輸入dot -version&#xff0c;出現相關信息即安裝成功 二、決策…

關于在頁面中針對不同版本的IE瀏覽器實現不同的JS或者CSS樣式

一般會用到<!--[if IE]>這里是正常的html代碼<![endif]--> 條件注釋只能在windows Internet Explorer(以下簡稱IE)下使用&#xff0c;因此我們可以通過條件注釋來為IE添加特別的指令。因為這只是IE瀏覽器支持的注釋。 1&#xff0c;條件注釋的基本結構和HTML的注釋…

機器學習筆記:PCA的簡單理解以及應用建議

用notability做的筆記&#xff0c;比較隨意&#xff0c;對于第五點的PCA錯誤使用需要特別強調。 目錄1、PCA與線性回歸2、PCA主成分數量選擇3、壓縮重現4、PCA應用建議5、PCA的錯誤使用1、PCA與線性回歸 2、PCA主成分數量選擇 3、壓縮重現 4、PCA應用建議 5、PCA的錯誤使用

關于asp.net中的錯誤提示“將截斷字符串或二進制數據。 語句已終止。”

好久沒有更新博客了&#xff0c;今天在寫asp.net網站的時候&#xff0c;出現了這個問題。錯誤提示“將截斷字符串或二進制數據。 語句已終止。”通過調試&#xff0c;發現在插入數據的時候&#xff0c;由于插入的數據的字符或者二進制數據的長度大于原來定義的類型的長度。及保…

c# 無法將類型隱式轉換_C#中的隱式類型數組

c# 無法將類型隱式轉換C&#xff03;隱式類型數組 (C# Implicitly Typed Arrays) Like implicitly typed variables, we can also declare an array without specifying its type such type of arrays are known as Implicitly typed arrays. 像隱式類型的變量一樣&#xff0c;…

一、信用卡卡號識別

一、思路分析 大體思路&#xff1a;首先拿到一張銀行卡&#xff0c;我們得有銀行卡號數字的0-9樣式的模板&#xff0c;然后再通過不同數字的輪廓的外接矩形來進行匹配&#xff0c;最終識別出銀行卡號所對應的數字。 銀行卡數字模板&#xff1a; 銀行卡信息&#xff1a; 拿到…

bootstrap網格系統_如何使用Bootstrap網格系統?

bootstrap網格系統In the last article, we learned how to create a simple page of Bootstrap? Now, we will learn what is "Grid System" in Bootstrap and how we can use or implement it in our bootstrap page? As you know bootstrap is a mobile-friendl…

有關WriteableBitmap和BitmapImage之間的相互轉換

對于WP7中圖形處理有關WriteableBitmap和BitmapImage之間的相互轉換&#xff0c;給大家幾個簡單實用的方法。一、WriteableBitmap轉為BitmapImage對象var bi new BitmapImage(); bi.SetSource(wb.ToImage().ToStream()); //其中wb是WriteableBitmap對象。 二、BitmapImage轉為…

回溯法初步

本文為參考公眾號所做的筆記。 代碼隨想錄原文 回溯法本質是窮舉&#xff0c;窮舉所有可能&#xff0c;然后選出我們想要的答案&#xff0c;所以它并不是一個高效的算法。但是由于有些問題本身能用暴力搜出來就不錯了&#xff0c;所以回溯法也有很多的應用。 回溯法解決的問題…

QEMU中smp,socket,cores,threads幾個參數的理解

在用QEMU創建KVM guest的時候&#xff0c;為了指定guest cpu資源&#xff0c;用到了-smp, -sockets, -cores, -threads幾個參數&#xff0c; #/usr/bin/qemu-system-x86_64 -name pqsfc085 -enable-kvm -m 2048 -smp 2,sockets2,cores1,threads1 \ -boot ordernc,onced \ -hda …