C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲
使用C++風格的數(shù)組,不需要管理內(nèi)存。
array要注意不要溢出,因為它是棧上開辟內(nèi)存.
array適用于任何類型
#include<iostream>
#include<array>
#include<vector>?? //C++的標準庫
#include<string>?? //C++字符串
#include <stdlib.h>
?
using? std::array; //靜態(tài)數(shù)組,棧上
using std::vector; //動態(tài)數(shù)組,堆上
using std::string;
?
//使用C++風格數(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:動態(tài)字符串數(shù)組
#include<iostream>
#include<array>
#include<vector>//C++的標準庫
#include<string>//C++字符串
#include <stdlib.h>
?
using? std::array;//靜態(tài)數(shù)組,棧上,
using std::vector;//動態(tài)數(shù)組,堆上,
using std::string;
?
//使用C++風格數(shù)組不需要管理內(nèi)存。
//array注意不要棧溢出
//array適用于任何類型
?
void main()
{
??? vector <string>? string1;//動態(tài)字符串數(shù)組
??? //可以反復利用
??? string1.push_back("notepad");
??? string1.push_back("calc");
??? string1.push_back("mspaint");
??? string1.pop_back();//刪除一個
??? //string1.clear();//清空
??? for (int i = 0; i < string1.size(); i++)//遍歷動態(tài)數(shù)組
??? {
??????? system(string1[i].c_str());
??? }
?
??? system("pause");
}
5.通過iterator關(guān)鍵字進行過迭代
#include<iostream>
#include<array>
#include<vector>?? //C++的標準庫
#include<string>?? //C++字符串
#include <stdlib.h>
?
using? std::array; //靜態(tài)數(shù)組,棧上
using std::vector; //動態(tài)數(shù)組,堆上
using std::string;
?
void main()
{
??? vector <string>? string1;//動態(tài)字符串數(shù)組
??? string1.push_back("notepad");
??? string1.push_back("calc");
??? string1.push_back("mspaint");
?
??? vector<string>::iterator ibegin, iend;//迭代器
??? ibegin = string1.begin();? //數(shù)據(jù)起始點
??? 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++的標準庫
#include<string>?? //C++字符串
#include <stdlib.h>
?
using? std::array; //靜態(tài)數(shù)組,棧上
using std::vector; //動態(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++的標準庫
#include<string>?? //C++字符串
#include <stdlib.h>
?
using? std::array; //靜態(tài)數(shù)組,棧上
using std::vector; //動態(tài)數(shù)組,堆上
using std::string;
?
void main()
{
??? vector<string> string1; //動態(tài)字符串數(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é)尾的下一個節(jié)點
??? //當使用下面的方法時,只打印了記事本,計算器
??? rend--;
??? A:if (rbegin != rend)
??? {
??????? system((*rend).c_str());? //執(zhí)行指令
??????? //rbegin++;
??????? rend--;
??????? goto A;
??? }
}
8.lambda表達式,不僅僅適用與array,也適用于vector
#include<iostream>
#include <vector>
#include <algorithm>? //算法 lambda表達式,不僅僅適用于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直接操作一個變量,res等價于返回值,x代表參數(shù),
??? //每次充當?shù)髦赶虻脑?#xff0c;大括號就是代碼
??? std::for_each(myvector.begin(), myvector.end(), [&res](int x){res += x; });
??? std::cout << res;
??? std::cin.get();
}
運行結(jié)果是:
9.vector的增刪改查
#include<iostream>
#include <vector>
#include <algorithm>? //算法 lambda表達式,不僅僅適用于array,也適用于vector
?
void main()
{
??? //分配5個空間,默認初始化為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);
??? //彈出一個元素,刪除最后一個
??? 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表達式,不僅僅適用于array,也適用于vector
?
void main()
{
??? //可以實現(xiàn)動態(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個數(shù)
??? std::array<double, 4> dbnew1 = { 10.1, 10.2, 10.3, 10.4 };
??? //可以實現(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();
}
運行結(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的高級,緩沖區(qū)
#include<iostream>
#include<new>
const int buf(512);//限定一個常量整數(shù)512
int N(5);//數(shù)組的長度
char buffer[buf] = { 0 };//靜態(tài)區(qū)
?
//p1,p3,p5作為指針變量在棧區(qū),存儲的地址指向堆區(qū)
//手動釋放內(nèi)存
?
//p2,p4,p6作為指針變量在棧區(qū),存儲的地址在靜態(tài)區(qū)。緩沖區(qū)。
//自動釋放內(nèi)存,用于分配用完了就不會再用的數(shù)據(jù)
//避免內(nèi)存泄漏,自動釋放內(nèi)存。犧牲了內(nèi)存訪問獨立性,
using namespace std;
?
void main()
{
??? double *p1, *p2;
?
??? std::cout << "\n\n\n";
??? p1 = new double[N];//分配內(nèi)存,N個元素的大小
??? p2 = new (buffer)double[N];//指定區(qū)域分配內(nèi)存
??? for (int i = 0; i < N; i++)
??? {
??????? p1[i] = p2[i] = i + 10.8;//對于數(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個元素的大小
??? p4 = new (buffer)double[N];//指定區(qū)域分配內(nèi)存
?
??? for (int i = 0; i < N; i++)
??? {
??????? p3[i] = p4[i] = i + 10.8;//對于數(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個元素的大小
??? p6 = new (buffer)double[N];//指定區(qū)域分配內(nèi)存
?
??? for (int i = 0; i < N; i++)
??? {
??????? p6[i] = p5[i] = i + 10.8;//對于數(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);//下標只能是常量
??? 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必須是一個靜態(tài)數(shù)組
//配合vector,array
?
總結(jié)
以上是生活随笔為你收集整理的C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 货车帮怎么删除车辆信息?
- 下一篇: 连杆 连杆 黑坑钓鲤鱼连杆小药?