1.vector 的数据的存入和输出:
#include<stdio.h>
#include<vector>
#include <iostream>
using namespace std
;
void main()
{
int i
= 0;
vector
<int> v
;
for( i
= 0; i
< 10; i
++ )
{
v
.push_back( i
);
}
对存入的数据清空
for( i
= 0; i
< v
.size(); i
++ )
{
cout
<< v
[ i
] << " ";
}
cont
<< endl
;
}
注:你也可以用v
.begin()和v
.end() 来得到vector开始的和结束的元素地址的指针位置。你也可以这样做:
vector
<int>::iterator iter
;
for( iter
= v
.begin(); iter
!= v
.end(); iter
++ )
{
cout
<< *iter
<< endl
;
}
2. 对于二维vector的定义。
1)定义一个
10个vector元素,并对每个vector符值
1-10。
#include<stdio.h>
#include<vector>
#include <iostream>
using namespace std
;
void main()
{
int i
= 0, j
= 0;
所以每一行的长度是可以变化的。之所以用到vector
<int>(0)是对vector初始化,否则不能对vector存入元素。
vector
< vector
<int> > Array( 10, vector
<int>(0) );
for( j
= 0; j
< 10; j
++ )
{
for ( i
= 0; i
< 9; i
++ )
{
Array
[ j
].push_back( i
);
}
}
for( j
= 0; j
< 10; j
++ )
{
for( i
= 0; i
< Array
[ j
].size(); i
++ )
{
cout
<< Array
[ j
][ i
] << " ";
}
cout
<< endl
;
}
}
2)定义一个行列都是变化的数组。
#include<stdio.h>
#include<vector>
#include <iostream>
using namespace std
;
void main()
{
int i
= 0, j
= 0;
vector
< vector
<int> > Array
;
vector
< int > line
;
for( j
= 0; j
< 10; j
++ )
{
Array
.push_back( line
);
for ( i
= 0; i
< 9; i
++ )
{
Array
[ j
].push_back( i
);
}
}
for( j
= 0; j
< 10; j
++ )
{
for( i
= 0; i
< Array
[ j
].size(); i
++ )
{
cout
<< Array
[ j
][ i
] << " ";
}
cout
<< endl
;
}
}
使用 vettor erase 指定元素
#include "iostream"
#include "vector"
using namespace std
;
int main()
{
vector
<int> arr
;
arr
.push_back(6);
arr
.push_back(8);
arr
.push_back(3);
arr
.push_back(8);
for(vector
<int>::iterator it
=arr
.begin(); it
!=arr
.end(); )
{
if(* it
== 8)
{
it
= arr
.erase(it
);
}
else
{
++it
;
}
}
cout
<< "After remove 8:/n";
for(vector
<int>::iterator it
= arr
.begin(); it
< arr
.end(); ++it
)
{
cout
<< * it
<< " ";
}
cout
<< endl
;
}
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std
;
void main()
{
int iarray
[]={0,1,2,3,4,5,6,6,6,7,8};
vector
<int> ivector(iarray
,iarray
+sizeof(iarray
)/sizeof(int));
int iarray1
[]={6,6};
vector
<int> ivector1(iarray1
,iarray1
+sizeof(iarray1
)/sizeof(int));
int iarray2
[]={5,6};
vector
<int> ivector2(iarray2
,iarray2
+sizeof(iarray2
)/sizeof(int));
int iarray3
[]={0,1,2,3,4,5,7,7,7,9,7};
vector
<int> ivector3(iarray3
,iarray3
+sizeof(iarray3
)/sizeof(int));
cout
<<*adjacent_find(ivector
.begin(),ivector
.end())<<endl
;
cout
<<count(ivector
.begin(),ivector
.end(),6)<<endl
;
cout
<<count_if(ivector
.begin(),ivector
.end(),bind2nd(less
<int>(),7))<<endl
;
cout
<<*find(ivector
.begin(),ivector
.end(),4)<<endl
;
cout
<<*find_if(ivector
.begin(),ivector
.end(),bind2nd(greater
<int>(),2))
<<endl
;
cout
<<*(find_end(ivector
.begin(),ivector
.end(),ivector1
.begin(),
ivector1
.end())+3)<<endl
;
cout
<<*(find_first_of(ivector
.begin(),ivector
.end(),ivector1
.begin(),
ivector1
.end())+3)<<endl
;
cout
<<*search(ivector
.begin(),ivector
.end(),ivector2
.begin(),ivector2
.end())
<<endl
;
cout
<<*search_n(ivector
.begin(),ivector
.end(),3,6,equal_to
<int>())<<endl
;
cout
<< equal(ivector
.begin(), ivector
.end(), ivector3
.begin()) << endl
;
pair
<int*,int*>result
=mismatch(ivector
.begin(),ivector
.end(),ivector3
.begin());
cout
<< result
.first
- ivector
.begin() << endl
;
}
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std
;
class even_by_two
{
public
:
int operator()() const
{return _x
+=2;}
private
:
static int _x
;
};
int even_by_two
::_x
=0;
void main()
{
int iarray
[]={0,1,2,3,4,5,6,6,6,7,8};
int iarray1
[]={0,1,2,3,4,4,5,5,6,6,6,6,6,7,8};
vector
<int> ivector(iarray
,iarray
+sizeof(iarray
)/sizeof(int));
vector
<int> ivector1(iarray
+6,iarray
+8);
vector
<int> ivector2(iarray1
,iarray1
+sizeof(iarray1
)/sizeof(int));
ostream_iterator
< int > output( cout
, " " );
generate(ivector1
.begin(),ivector1
.end(),even_by_two());
copy(ivector1
.begin(),ivector1
.end(),output
);
cout
<<endl
;
generate_n(ivector
.begin(),3,even_by_two());
copy(ivector
.begin(),ivector
.end(),output
);
cout
<<endl
;
remove(ivector
.begin(),ivector
.end(),6);
copy(ivector
.begin(),ivector
.end(),output
);
cout
<<endl
;
vector
<int> ivector3(12);
remove_copy(ivector
.begin(),ivector
.end(),ivector3
.begin(),6);
copy(ivector3
.begin(),ivector3
.end(),output
);
cout
<<endl
;
remove_if(ivector
.begin(),ivector
.end(),bind2nd(less
<int>(),6));
copy(ivector
.begin(),ivector
.end(),output
);
cout
<<endl
;
remove_copy_if(ivector
.begin(),ivector
.end(),ivector3
.begin(),
bind2nd(less
<int>(),7));
copy(ivector3
.begin(),ivector3
.end(),output
);
cout
<<endl
;
replace(ivector
.begin(),ivector
.end(),6,3);
copy(ivector
.begin(),ivector
.end(),output
);
cout
<<endl
;
replace_copy(ivector
.begin(),ivector
.end(),ivector3
.begin(),3,5);
copy(ivector3
.begin(),ivector3
.end(),output
);
cout
<<endl
;
replace_if(ivector
.begin(),ivector
.end(),bind2nd(less
<int>(),5),2);
copy(ivector
.begin(),ivector
.end(),output
);
cout
<<endl
;
replace_copy_if(ivector
.begin(),ivector
.end(),ivector3
.begin(),
bind2nd(equal_to
<int>(),8),9);
copy(ivector3
.begin(),ivector3
.end(),output
);
cout
<<endl
;
reverse(ivector
.begin(),ivector
.end());
copy(ivector
.begin(),ivector
.end(),output
);
cout
<<endl
;
reverse_copy(ivector
.begin(),ivector
.end(),ivector3
.begin());
copy(ivector3
.begin(),ivector3
.end(),output
);
cout
<<endl
;
rotate(ivector
.begin(),ivector
.begin()+4,ivector
.end());
copy(ivector
.begin(),ivector
.end(),output
);
cout
<<endl
;
rotate_copy(ivector
.begin(),ivector
.begin()+5,ivector
.end(),
ivector3
.begin());
copy(ivector3
.begin(),ivector3
.end(),output
);
cout
<<endl
;
}
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std
;
void main()
{
int iarray
[]={26,17,15,22,23,33,32,40};
vector
<int> ivector(iarray
,iarray
+sizeof(iarray
)/sizeof(int));
cout
<<*max_element(ivector
.begin(),ivector
.end())<<endl
;
cout
<<*min_element(ivector
.begin(),ivector
.end())<<endl
;
partial_sort(ivector
.begin(),ivector
.begin()+3,ivector
.end());
copy(ivector
.begin(),ivector
.end(),ostream_iterator
<int>(cout
," "));
cout
<<endl
;
vector
<int> ivector1(5);
partial_sort_copy(ivector
.begin(),ivector
.end(),ivector1
.begin(),
ivector1
.end());
copy(ivector1
.begin(),ivector1
.end(),ostream_iterator
<int>(cout
," "));
cout
<<endl
;
sort(ivector
.begin(),ivector
.end());
copy(ivector
.begin(),ivector
.end(),ostream_iterator
<int>(cout
," "));
cout
<<endl
;
cout
<<*lower_bound(ivector
.begin(),ivector
.end(),24)<<endl
;
cout
<<*upper_bound(ivector
.begin(),ivector
.end(),24)<<endl
;
cout
<<binary_search(ivector
.begin(),ivector
.end(),33)<<endl
;
cout
<<binary_search(ivector
.begin(),ivector
.end(),34)<<endl
;
next_permutation(ivector
.begin(),ivector
.end());
copy(ivector
.begin(),ivector
.end(),ostream_iterator
<int>(cout
," "));
cout
<<endl
;
prev_permutation(ivector
.begin(),ivector
.end());
copy(ivector
.begin(),ivector
.end(),ostream_iterator
<int>(cout
," "));
cout
<<endl
;
vector
<int> ivector2(13);
merge(ivector
.begin(),ivector
.end(),ivector1
.begin(),ivector1
.end(),
ivector2
.begin());
copy(ivector2
.begin(),ivector2
.end(),ostream_iterator
<int>(cout
," "));
cout
<<endl
;
nth_element(ivector2
.begin(),ivector2
.begin()+5,ivector2
.end());
copy(ivector2
.begin(),ivector2
.end(),ostream_iterator
<int>(cout
," "));
cout
<<endl
;
stable_sort(ivector2
.begin(),ivector2
.end());
copy(ivector2
.begin(),ivector2
.end(),ostream_iterator
<int>(cout
," "));
cout
<<endl
;
pair
<vector
<int>::iterator
,vector
<int>::iterator
> pairIte
;
pairIte
=equal_range(ivector2
.begin(),ivector2
.end(),22);
cout
<<*(pairIte
.first
)<<endl
;
cout
<<*(pairIte
.second
)<<endl
;
int iarray3
[] = { 1, 3, 5, 7, 2, 4, 6, 8 };
vector
<int> ivector3(iarray3
,iarray3
+sizeof(iarray3
)/sizeof(int));
inplace_merge(ivector3
.begin(), ivector3
.begin()+ 4, ivector3
.end());
copy(ivector3
.begin(),ivector3
.end(), ostream_iterator
<int>(cout
, " "));
cout
<<endl
;
int iarray4
[] = { 1, 3, 5, 7,1, 5, 9, 3 };
vector
<int> ivector4(iarray4
,iarray4
+sizeof(iarray4
)/sizeof(int));
cout
<< lexicographical_compare(ivector3
.begin(),ivector3
.end(),
ivector4
.begin(),ivector4
.end()) << endl
}
---------------------
作者:wanglibaocsde
来源:
原文:https
://blog
.csdn
.net
/wanglibaocsde
/article
/details
/6133020
版权声明:本文为博主原创文章,转载请附上博文链接!