// ConsoleApplication2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<vector>
#include<cstdio>
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
vector<double> a;
/*unsigned n;
cout << "n = ";
*/
vector<double> K = {1.0,2.0,3.0};
for (auto i = K.begin(); i != K.end(); ++i)
std::cout << *i << endl;
for (auto e : K)
cout << e ;
cout <<endl;
//向量a中添加元素
vector<int>Ba;
for (int i = 0; i<10; ++i){
Ba.push_back(i);
}
for (auto e : Ba)
cout << e;
cout <<"Ba" <<endl;
Sleep(1);
//vector<int>array(10);
//定义具有10个整型元素的向量(尖括号为元素类型名,
//它可以是任何合法的数据类型),不具有初值,其值不确定
vector<int>array(10,10);
//定义具有10个整型元素的向量,且给出的每个元素初值为1
//vector<int>a(10, 1);
//用向量b给向量a赋值,a的值完全等价于b的值
//vector<int>a(b);
vector<int>array1(array);
for (auto abc : array1)
cout << abc;
cout << endl;
for (int i = 0; i<10; ++i) {
array1.push_back(i);
}
for (auto abcd : array1)
cout << abcd;
cout << endl;
将向量b中从0-2(共三个)的元素赋值给a,a的类型为int型
//vector<int>a(b.begin(), b.begin + 3);
vector<double>fuzhi(array1.begin(),array1.end()-1);
for (int i=0; i!=fuzhi.size();++i)
cout << fuzhi[i];
cout << endl;
int b[8] = { 1,2,3,4,5,6,7,8 };
vector<int>tmd(b,b+7);
for (int i = 0; i != tmd.size(); ++i)
cout << tmd[i];
//从数组中获得初值
//int b[7] = { 1,2,3,4,5,6,7 };
//vector<int> a(b, b + 7);
cout << endl;
//bool ddd = K.clear();
// assign 赋值函数
vector<int> V(10, 0);
double amd[10] = { 1.,2.,3.,4.,5.,6.,7.,8.,9.,10.};
V.assign(amd, amd + 5);
for (auto nvida : V)
cout << nvida;
cout << endl;
if(tmd.empty()){
for (auto nvida1 : tmd)
cout << nvida1;
}
for (auto nvida1 : V)
cout << nvida1;
cout << endl;
V.pop_back();
//V.pop_back();//该函数不接受一个参数
//V.pop_back();//删除第一个元素 其余元素向前排列
for (auto nvida2 : V)
cout << nvida2;
cout << endl;
V.erase(V.begin()+ 1, V.begin() + 2);//删除指定位置 指定个数的元素
//这个比较有用 niubi
for (auto nvida2 : V)
cout << nvida2;
cout << endl;
cout << V.size();
cout << endl;
//cout << V.capacity();
V.resize(10,2);
V.reserve(100);
/*for (auto nvida3 : V)
cout << nvida3;*/
for (int i = 0; i < V.size(); i++)
cout << V[i];
cout << endl;
cout << V.back();
cout << endl;
cout << V.front();
cout << endl;
//插入操作
V.insert(V.begin() + 2, 9);
V.push_back(8);
cout <<endl;
V.insert(V.begin() + 5,3,5);
for (int i = 0; i < V.size(); i++)
cout << V[i];
cout << endl;
V.insert(V.begin() + 4,Ba.begin()+6, Ba.begin() + 9);
for (int i = 0; i < V.size(); i++)
cout << V[i];
cout << endl;
cout << V.capacity()<<endl;
V.swap(Ba);
for (int i = 0; i < V.size(); i++)
cout << V[i];
cout <<"V" <<endl;
for (int i = 0; i < Ba.size(); i++)
cout << Ba[i];
cout <<"Ba" <<endl;
//swap 直接交换两个vector 不需要大小相等
// == 比较两个向量是否相等
cout << (Ba == V) << endl;
cout << (Ba >= V) << endl;
cout << (Ba <= V) << endl;
cout << (Ba != V) << endl;
int AA[15] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
vector<int> AB;
vector<int> ABC(AA,AA+5);
vector<int> ABCD1(AA + 8, AA + 14);
vector<int> ABCD2(AA, AA + 7);
for (vector<int>::iterator it = ABC.begin(); it < ABC.end(); it++)
{
AB.push_back(*it);
cout << "ABY" ;
cout << (*it) << endl;
}
for (int i = 0; i < AB.size(); i++)
cout << AB[i];
cout << "AB" << endl;
cout << endl;
ABCD1.swap(ABCD2);//swap 只能交换 数据类型相同的vector向量
for (int i = 0; i < ABCD1.size(); ++i)
cout << ABCD1[i];
cout << ":-1" << endl;
for (int i = 0; i < ABCD2.size(); i++)
cout << ABCD2[i];
cout << ":-2" << endl;
return 0;
//下面为迭代器方式读取操作
//int a[6] = { 1,2,3,4,5,6 };
//vector<int>b(a, a + 4);
//for (vector<int>::iterator it = b.begin(); it != b.end(); it++) { cout << *it << " "; }
}