标签:style blog http color 使用 os io 数据
使用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 << "\n";
}
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.通过iterator关键字进行过迭代
#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 << "\n";
}
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 << "\n\n\n";
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 << "\n\n\n";
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 << "\n\n\n";
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
C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲,布布扣,bubuko.com
C++中的数组array和vector,lambda表达式,C字符串加操作,C++中新类型数组(数组缓存),多元数组,new缓冲
标签:style blog http color 使用 os io 数据
原文地址:http://blog.csdn.net/tototuzuoquan/article/details/38627111