-
使用C++风格的数组。不须要管理内存。
-
array要注意不要溢出,由于它是栈上开辟内存.
-
array适用于不论什么类型
#include<iostream>
#include<array>
#include<vector> //C++的标准库
#include<string> //C++字符串
#include <stdlib.h>
using std::array; //静态数组,栈上
using std::vector; //动态数组,堆上
using std::string;
//使用C++风格数组不须要管理内存。
//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++)//数组大小
{
for (int j = 0; j < myint1.size(); j++)
{
std::cout << " " << myint[i][j];
}
std::cout << " ";
}
std::cin.get();
}
-
vector:动态字符串数组
#include<iostream>
#include<array>
#include<vector>//C++的标准库
#include<string>//C++字符串
#include <stdlib.h>
using std::array;//静态数组,栈上,
using std::vector;//动态数组,堆上,
using std::string;
//使用C++风格数组不须要管理内存。
//array注意不要栈溢出
//array适用于不论什么类型
void main()
{
vector <string> string1;//动态字符串数组
//能够重复利用
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++)//遍历动态数组
{
system(string1[i].c_str());
}
system("pause");
}
5.通过iteratorkeyword进行过迭代
#include<iostream>
#include<array>
#include<vector> //C++的标准库
#include<string> //C++字符串
#include <stdlib.h>
using std::array; //静态数组,栈上
using std::vector; //动态数组。堆上
using std::string;
void main()
{
vector <string> string1;//动态字符串数组
string1.push_back("notepad");
string1.push_back("calc");
string1.push_back("mspaint");
vector<string>::iterator ibegin, iend;//迭代器
ibegin = string1.begin(); //数据起始点
iend = string1.end(); //结束
for (;ibegin != iend;ibegin++)
{
string tempstr = *ibegin; //获取指针指向的数据
system(tempstr.c_str()); //运行指令
}
system("pauese");
}
6.正逆向迭代数组
#include<iostream>
#include<array>
#include<vector> //C++的标准库
#include<string> //C++字符串
#include <stdlib.h>
using std::array; //静态数组。栈上
using std::vector; //动态数组,堆上
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; //静态数组。栈上
using std::vector; //动态数组,堆上
using std::string;
void main()
{
vector<string> string1; //动态字符串数组
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最后不指向数据,指向数据的结尾的下一个节点
//当使用以下的方法时,仅仅打印了记事本,计算器
rend--;
A:if (rbegin != rend)
{
system((*rend).c_str()); //运行指令
//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; //结果
//&res直接操作一个变量。res等价于返回值,x代表參数,
//每次充当迭代器指向的元素,大括号就是代码
std::for_each(myvector.begin(), myvector.end(), [&res](int x){res += x; });
std::cout << res;
std::cin.get();
}
执行结果是:
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);
//依据迭代器的位置
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()
{
//能够实现动态无规则数组管理
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 << " ";
}
std::cin.get();
}
11.C++中的数组能够直接赋值
#include <iostream>
#include <array>
#include <string>
#include <stdlib.h>
void main()
{
double db[4] = { 1.1, 2.2, 3.3, 4.4 };
//std::array数据类型,double元素类型,4个数
std::array<double, 4> dbnew1 = { 10.1, 10.2, 10.3, 10.4 };
//能够实现数组之间总体操作
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();
}
执行结果:
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的高级,缓冲区
#include<iostream>
#include<new>
const int buf(512);//限定一个常量整数512
int N(5);//数组的长度
char buffer[buf] = { 0 };//静态区
//p1,p3,p5作为指针变量在栈区,存储的地址指向堆区
//手动释放内存
//p2,p4,p6作为指针变量在栈区,存储的地址在静态区。缓冲区。
//自己主动释放内存,用于分配用完了就不会再用的数据
//避免内存泄漏,自己主动释放内存。牺牲了内存訪问独立性。
using namespace std;
void main()
{
double *p1, *p2;
std::cout << " ";
p1 = new double[N];//分配内存,N个元素的大小
p2 = new (buffer)double[N];//指定区域分配内存
for (int i = 0; i < N; i++)
{
p1[i] = p2[i] = i + 10.8;//对于数组初始化
std::cout << "p1=== " << &p1[i] << " " << p1[i];
std::cout << " p2=== "<< &p2[i] << " " << p2[i] << std::endl;
}
double *p3, *p4;
std::cout << " ";
p3 = new double[N];//分配内存,N个元素的大小
p4 = new (buffer)double[N];//指定区域分配内存
for (int i = 0; i < N; i++)
{
p3[i] = p4[i] = i + 10.8;//对于数组初始化
std::cout << "p3=== " << &p3[i] << " " << p3[i];
std::cout << " p4=== "<< &p4[i] << " " << p4[i] << std::endl;
}
double *p5, *p6;
std::cout << " ";
p5 = new double[N];//分配内存。N个元素的大小
p6 = new (buffer)double[N];//指定区域分配内存
for (int i = 0; i < N; i++)
{
p6[i] = p5[i] = i + 10.8;//对于数组初始化
std::cout << "p5=== " << &p5[i] << " " << p5[i];
std::cout << " p6=== "<< &p6[i] << " " << p6[i] << std::endl;
}
std::cin.get();
}
14.多元数组
#include <iostream>
#include <map>
//void在參数内部意味着參数为空,不写也意味着为空
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; //获取数据类型再次创建
//mytuple.swap(mytuple); array vector都有交换的功能
std::cout << data0 << " " << data1 << " " << data2 << " " << data3 << std::endl;
std::cin.get();
}
//tuple必须是一个静态数组
//配合vector,array