日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

【C++grammar】vector类和字符串字面量

發(fā)布時間:2023/12/1 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【C++grammar】vector类和字符串字面量 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

C++的vector類

用數(shù)組存放數(shù)據(jù)時,容量大小不可變,vector對象容量可自動增大。
vector的操作:

調(diào)用push_back函數(shù)時,vector對象的容量可能會增大。
觀察下列操作對vector的影響:

#include <vector> #include <iostream> #include <string>#include "Helper.h" int main() {//用c++11的列表初始化,創(chuàng)建vector對象words1std::cout<< "用c++11的列表初始化,創(chuàng)建vector對象words1"<< std::endl;std::vector<std::string> words1{"Hello","World!","Welcome","To","C!"};PRINT(words1);std::cout << std::endl;//刪除words1最后一個元素std::cout << "刪除words1最后一個元素" << std::endl;words1.erase(words1.end()-1);PRINT(words1);//在words1尾部追加元素std::cout << "在words1尾部追加元素" << std::endl;words1.push_back("C++!");PRINT(words1);std::cout << std::endl;//用迭代器拷貝words1的內(nèi)容以創(chuàng)建words2std::cout << "1、用迭代器拷貝words1的內(nèi)容以創(chuàng)建words2" << std::endl;std::vector<std::string> words2(words1.begin()+2, words1.end());PRINT(words2);std::cout << std::endl;//在words中插入元素std::cout << "2、在words中插入元素" << std::endl;words2.insert(words2.begin(),"Hello!");PRINT(words2);std::cout << std::endl;//用拷貝構(gòu)造創(chuàng)建words3std::cout << "3、用拷貝構(gòu)造創(chuàng)建words3" << std::endl;std::vector<std::string> words3(words2);PRINT(words3);std::cout << std::endl;//用[]修改words的元素std::cout << "4、用[]修改words的元素" << std::endl;words3[3] = "C Plus Plus";PRINT(words3);std::cout << std::endl;//創(chuàng)建words4,初始化為多個相同的字串std::cout << "5、創(chuàng)建words4,初始化為多個相同的字串" << std::endl;std::vector<std::string> words4(4, "C++!");PRINT(words4);std::cout << std::endl;//words3與words4交換std::cout << "6、words3與words4交換" << std::endl;words3.swap(words4);PRINT(words3);PRINT(words4);std::cout << std::endl;return 0; }

字符串字面量

C++11原始字符串字面量

語法: R “delimiter( raw_characters )delimiter”
“Raw String literals”在程序中寫成什么樣子,輸出之后就是什么樣子。我們不需要為“Raw String literals”中的換行、雙引號等特殊字符進行轉(zhuǎn)義。

#include <vector>#include <iostream> const char* s1 = R"(Hello World)";// s1效果與下面的s2和s3相同const char* s2 = "Hello\nWorld";const char* s3 = R"NoUse(Hello World)NoUse";int main() {std::cout << s1 << std::endl;std::cout <<std::endl;std::cout << s2 << std::endl;std::cout << std::endl;std::cout << s3 << std::endl;}

C++14的字符串字面量

C++14將運算符 ""s 進行了重載,賦予了它新的含義,使得用這種運算符括起來的字符串字面量,自動變成了一個 std::string 類型的對象。

auto hello = "Hello!"s; // hello is of std::string type auto hello = std::string{"Hello!"}; // equals to the above auto hello = "Hello!"; // hello is of const char* type

已知’\0’在字符串里面代表結(jié)束符號

#include <string> #include <iostream> int main() {using namespace std::string_literals;std::string s1 = "abc\0\0def";std::string s2 = "abc\0\0def"s;std::cout << "s1: " << s1.size() << " \"" << s1 << "\"\n";std::cout << "s2: " << s2.size() << " \"" << s2 << "\"\n"; }

總結(jié)

以上是生活随笔為你收集整理的【C++grammar】vector类和字符串字面量的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。