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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲

發(fā)布時(shí)間:2024/9/27 c/c++ 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

  • 使用C++風(fēng)格的數(shù)組,不需要管理內(nèi)存。

  • array要注意不要溢出,因?yàn)樗菞I祥_辟內(nèi)存.

  • array適用于任何類型

  • #include<iostream>

    #include<array>

    #include<vector>?? //C++的標(biāo)準(zhǔn)庫

    #include<string>?? //C++字符串

    #include <stdlib.h>

    ?

    using? std::array; //靜態(tài)數(shù)組,棧上

    using std::vector; //動(dòng)態(tài)數(shù)組,堆上

    using std::string;

    ?

    //使用C++風(fēng)格數(shù)組不需要管理內(nèi)存。

    //array注意不要棧溢出

    //array適用于任何類型

    ?

    void main()

    {

    ??? array<int, 5> myint1 = { 1, 2, 3, 4, 5 };

    ??? array<int, 5> myint2 = { 11, 12, 13, 14, 15 };

    ??? array<int, 5> myint3 = { 21, 22, 23, 24, 25 };

    ??? //? array<array<int,5>, 3> myint = {myint1,myint2,myint3};

    ??? array<array<int, 5>, 3> myint = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

    ??? for (int i = 0; i < myint.size(); i++)//數(shù)組大小

    ??? {

    ??????? for (int j = 0; j < myint1.size(); j++)

    ??????? {

    ??????????? std::cout << "? " << myint[i][j];

    ??????? }

    ??????? std::cout << "\n";

    ??? }

    ?

    ??? std::cin.get();

    }

  • vector:動(dòng)態(tài)字符串?dāng)?shù)組

  • #include<iostream>

    #include<array>

    #include<vector>//C++的標(biāo)準(zhǔn)庫

    #include<string>//C++字符串

    #include <stdlib.h>

    ?

    using? std::array;//靜態(tài)數(shù)組,棧上,

    using std::vector;//動(dòng)態(tài)數(shù)組,堆上,

    using std::string;

    ?

    //使用C++風(fēng)格數(shù)組不需要管理內(nèi)存。

    //array注意不要棧溢出

    //array適用于任何類型

    ?

    void main()

    {

    ??? vector <string>? string1;//動(dòng)態(tài)字符串?dāng)?shù)組

    ??? //可以反復(fù)利用

    ??? string1.push_back("notepad");

    ??? string1.push_back("calc");

    ??? string1.push_back("mspaint");

    ??? string1.pop_back();//刪除一個(gè)

    ??? //string1.clear();//清空

    ??? for (int i = 0; i < string1.size(); i++)//遍歷動(dòng)態(tài)數(shù)組

    ??? {

    ??????? system(string1[i].c_str());

    ??? }

    ?

    ??? system("pause");

    }

    5.通過iterator關(guān)鍵字進(jìn)行過迭代

    #include<iostream>

    #include<array>

    #include<vector>?? //C++的標(biāo)準(zhǔn)庫

    #include<string>?? //C++字符串

    #include <stdlib.h>

    ?

    using? std::array; //靜態(tài)數(shù)組,棧上

    using std::vector; //動(dòng)態(tài)數(shù)組,堆上

    using std::string;

    ?

    void main()

    {

    ??? vector <string>? string1;//動(dòng)態(tài)字符串?dāng)?shù)組

    ??? string1.push_back("notepad");

    ??? string1.push_back("calc");

    ??? string1.push_back("mspaint");

    ?

    ??? vector<string>::iterator ibegin, iend;//迭代器

    ??? ibegin = string1.begin();? //數(shù)據(jù)起始點(diǎn)

    ??? iend = string1.end();????? //結(jié)束

    ??? for (;ibegin != iend;ibegin++)

    ??? {

    ??????? string tempstr = *ibegin;?? //獲取指針指向的數(shù)據(jù)

    ??????? system(tempstr.c_str());??? //執(zhí)行指令

    ??? }

    ?

    ??? system("pauese");

    }

    6.正逆向迭代數(shù)組

    #include<iostream>

    #include<array>

    #include<vector>?? //C++的標(biāo)準(zhǔn)庫

    #include<string>?? //C++字符串

    #include <stdlib.h>

    ?

    using? std::array; //靜態(tài)數(shù)組,棧上

    using std::vector; //動(dòng)態(tài)數(shù)組,堆上

    using std::string;

    ?

    void main()

    {

    ??? array<int, 5> myint = { 1, 2, 3, 4, 5 };

    ??? array<int, 5>::iterator ibegin, iend;//正向迭代器

    ??? ibegin = myint.begin();

    ??? iend = myint.end();

    ??? while (ibegin != iend)

    ??? {

    ??????? std::cout << *ibegin << std::endl;

    ??????? ibegin++;

    ??? }

    ??? std::cout << "----------" << std::endl;

    ??? array<int, 5>::reverse_iterator rbegin, rend;

    ??? rbegin = myint.rbegin();

    ??? rend = myint.rend();

    ??? while (rbegin != rend)

    ??? {

    ??????? std::cout << *rbegin << std::endl;

    ??????? rbegin++;

    ??? }

    ??? std::cin.get();

    }

    7.反向迭代器

    #include<iostream>

    #include<array>

    #include<vector>?? //C++的標(biāo)準(zhǔn)庫

    #include<string>?? //C++字符串

    #include <stdlib.h>

    ?

    using? std::array; //靜態(tài)數(shù)組,棧上

    using std::vector; //動(dòng)態(tài)數(shù)組,堆上

    using std::string;

    ?

    void main()

    {

    ??? vector<string> string1; //動(dòng)態(tài)字符串?dāng)?shù)組

    ??? string1.push_back("notepad");

    ??? string1.push_back("calc");

    ??? string1.push_back("mspaint");

    ??? //反向迭代器

    ??? vector<string>::reverse_iterator rbegin = string1.rbegin();

    ??? vector<string>::reverse_iterator rend = string1.rend();

    ??? //rend最后不指向數(shù)據(jù),指向數(shù)據(jù)的結(jié)尾的下一個(gè)節(jié)點(diǎn)

    ??? //當(dāng)使用下面的方法時(shí),只打印了記事本,計(jì)算器

    ??? rend--;

    ??? A:if (rbegin != rend)

    ??? {

    ??????? system((*rend).c_str());? //執(zhí)行指令

    ??????? //rbegin++;

    ??????? rend--;

    ??????? goto A;

    ??? }

    }

    8.lambda表達(dá)式,不僅僅適用與array,也適用于vector

    #include<iostream>

    #include <vector>

    #include <algorithm>? //算法 lambda表達(dá)式,不僅僅適用于array,也適用于vector

    ?

    void main()

    {

    ??? std::vector<int> myvector;

    ??? myvector.push_back(11);

    ??? myvector.push_back(22);

    ??? myvector.push_back(33);

    ??? myvector.push_back(3);

    ??? myvector.push_back(4);

    ??? myvector.push_back(5);

    ??? int res = 0;? //結(jié)果

    ??? //&res直接操作一個(gè)變量,res等價(jià)于返回值,x代表參數(shù),

    ??? //每次充當(dāng)?shù)髦赶虻脑?#xff0c;大括號(hào)就是代碼

    ??? std::for_each(myvector.begin(), myvector.end(), [&res](int x){res += x; });

    ??? std::cout << res;

    ??? std::cin.get();

    }

    運(yùn)行結(jié)果是:

    9.vector的增刪改查

    #include<iostream>

    #include <vector>

    #include <algorithm>? //算法 lambda表達(dá)式,不僅僅適用于array,也適用于vector

    ?

    void main()

    {

    ??? //分配5個(gè)空間,默認(rèn)初始化為0

    ??? std::vector<int> myvector(5);

    ??? myvector.push_back(1);

    ??? myvector.push_back(11);

    ??? myvector.push_back(111);

    ??? myvector.push_back(1111);

    ??? myvector.push_back(2);

    ??? //彈出一個(gè)元素,刪除最后一個(gè)

    ??? myvector.pop_back();

    ??? //插入

    ??? myvector.insert(myvector.begin() + 1,999);

    ??? //根據(jù)迭代器的位置

    ??? myvector.erase(myvector.begin() + 5);

    ??? //myvector.clear(); //刪除所有元素

    ??? for (int i = 0; i < myvector.size();i++)

    ??? {

    ??????? if (1)

    ??????? {

    ??????????? //查詢,修改

    ??????? }

    ??????? std::cout << myvector.at(i) << std::endl;

    ??? }

    ??? system("pause");

    }

    10.vector中的元素也是vector

    #include<iostream>

    #include <vector>

    #include <algorithm>? //算法 lambda表達(dá)式,不僅僅適用于array,也適用于vector

    ?

    void main()

    {

    ??? //可以實(shí)現(xiàn)動(dòng)態(tài)無規(guī)則數(shù)組管理

    ??? std::vector<int> myvector1;

    ??? myvector1.push_back(12);

    ??? myvector1.push_back(13);

    ??? myvector1.push_back(14);

    ?

    ??? std::vector<int> myvector2;

    ??? myvector2.push_back(22);

    ?

    ??? std::vector<int> myvector3;

    ??? myvector3.push_back(32);

    ??? myvector3.push_back(37);

    ?

    ??? std::vector<std::vector<int>> allvecor;

    ??? allvecor.push_back(myvector1);

    ??? allvecor.push_back(myvector2);

    ??? allvecor.push_back(myvector3);

    ??? for (int i = 0; i < allvecor.size();i++)

    ??? {

    ??????? for (int j = 0; j < allvecor[i].size();j++)

    ??????? {

    ??????????? std::cout << "? " << allvecor[i][j];

    ??????? }

    ??????? std::cout << "\n";

    ??? }

    ?

    ??? std::cin.get();

    }

    11.C++中的數(shù)組可以直接賦值

    #include <iostream>

    #include <array>

    #include <string>

    #include <stdlib.h>

    ?

    void main()

    {

    ??? double db[4] = { 1.1, 2.2, 3.3, 4.4 };

    ??? //std::array數(shù)據(jù)類型,double元素類型,4個(gè)數(shù)

    ??? std::array<double, 4> dbnew1 = { 10.1, 10.2, 10.3, 10.4 };

    ??? //可以實(shí)現(xiàn)數(shù)組之間整體操作

    ??? std::array<double, 4> dbnew2 = dbnew1;

    ??? for (int i = 0; i < 4; i++)

    ??? {

    ??????? std::cout << db[i] << "?? " << dbnew1[i] << "??? " << dbnew2[i] << std::endl;

    ??? }

    ??? std::cin.get();

    }

    運(yùn)行結(jié)果:

    12.array的字符串

    #include <iostream>

    #include<array>

    #include<string>

    #include<stdlib.h>

    ?

    void main()

    {

    ??? std::array<std::string, 5> string1 = { "calc", "notepad", "tasklist", "mspaint", "write" };

    ??? for (int i = 0; i < 5; i++)

    ??? {

    ??????? std::cout << string1[i] << std::endl;

    ??????? system(string1[i].c_str());

    ??? }

    ??? std::cin.get();

    }

    12.C++可以加操作

    #include <iostream>

    #include<array>

    #include<string>

    #include<stdlib.h>

    ?

    void main()

    {

    ??? std::string str1 = "task";

    ??? std::string str2 = "list";

    ??? std::string str3 = str1 + str2;

    ??? system(str3.c_str());

    ??? std::cin.get();

    }

    13.new的高級(jí),緩沖區(qū)

    #include<iostream>

    #include<new>

    const int buf(512);//限定一個(gè)常量整數(shù)512

    int N(5);//數(shù)組的長度

    char buffer[buf] = { 0 };//靜態(tài)區(qū)

    ?

    //p1,p3,p5作為指針變量在棧區(qū),存儲(chǔ)的地址指向堆區(qū)

    //手動(dòng)釋放內(nèi)存

    ?

    //p2,p4,p6作為指針變量在棧區(qū),存儲(chǔ)的地址在靜態(tài)區(qū)。緩沖區(qū)。

    //自動(dòng)釋放內(nèi)存,用于分配用完了就不會(huì)再用的數(shù)據(jù)

    //避免內(nèi)存泄漏,自動(dòng)釋放內(nèi)存。犧牲了內(nèi)存訪問獨(dú)立性,

    using namespace std;

    ?

    void main()

    {

    ??? double *p1, *p2;

    ?

    ??? std::cout << "\n\n\n";

    ??? p1 = new double[N];//分配內(nèi)存,N個(gè)元素的大小

    ??? p2 = new (buffer)double[N];//指定區(qū)域分配內(nèi)存

    ??? for (int i = 0; i < N; i++)

    ??? {

    ??????? p1[i] = p2[i] = i + 10.8;//對(duì)于數(shù)組初始化

    ??????? std::cout << "p1===?? " << &p1[i] << "? " << p1[i];

    ??????? std::cout << "?? p2===??"<< &p2[i] << "? " << p2[i] << std::endl;

    ??? }

    ?

    ??? double *p3, *p4;

    ??? std::cout << "\n\n\n";

    ??? p3 = new double[N];//分配內(nèi)存,N個(gè)元素的大小

    ??? p4 = new (buffer)double[N];//指定區(qū)域分配內(nèi)存

    ?

    ??? for (int i = 0; i < N; i++)

    ??? {

    ??????? p3[i] = p4[i] = i + 10.8;//對(duì)于數(shù)組初始化

    ??????? std::cout << "p3===?? " << &p3[i] << "? " << p3[i];

    ??????? std::cout << "?? p4===??"<< &p4[i] << "? " << p4[i] << std::endl;

    ??? }

    ?

    ??? double *p5, *p6;

    ??? std::cout << "\n\n\n";

    ??? p5 = new double[N];//分配內(nèi)存,N個(gè)元素的大小

    ??? p6 = new (buffer)double[N];//指定區(qū)域分配內(nèi)存

    ?

    ??? for (int i = 0; i < N; i++)

    ??? {

    ??????? p6[i] = p5[i] = i + 10.8;//對(duì)于數(shù)組初始化

    ??????? std::cout << "p5===?? " << &p5[i] << "? " << p5[i];

    ??????? std::cout << "?? p6===??"<< &p6[i] << "? " << p6[i] << std::endl;

    ??? }

    ?

    ??? std::cin.get();

    }

    14.多元數(shù)組

    #include <iostream>

    #include <map>

    ?

    //void在參數(shù)內(nèi)部意味著參數(shù)為空,不寫也意味著為空

    void main(void)

    {

    ??? int int1 = 10;

    ??? double double1 = 99.8;

    ??? char ch = 'A';

    ??? char *str = "hellochina";

    ??? std::tuple<int, double, char, const char *> mytuple(int1, double1, ch, str);

    ??? const int num = 3;

    ??? auto data0 = std::get<0>(mytuple);

    ??? auto data1 = std::get<1>(mytuple);

    ??? auto data2 = std::get<2>(mytuple);

    ??? auto data3 = std::get<num>(mytuple);//下標(biāo)只能是常量

    ??? std::cout << typeid(data3).name() << std::endl;

    ??? decltype(data0) dataA;?? //獲取數(shù)據(jù)類型再次創(chuàng)建

    ??? //mytuple.swap(mytuple);?array? vector都有交換的功能

    ??? std::cout << data0 << "? " << data1 << "? " << data2 << "?? " << data3 << std::endl;

    ??? std::cin.get();

    }

    //tuple必須是一個(gè)靜態(tài)數(shù)組

    //配合vector,array

    ?

    總結(jié)

    以上是生活随笔為你收集整理的C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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