(原創) 如何将array转成std::vector? (使用constructor) (C/C++) (STL)
由于C++兼容于C,为了用C++维护以前用C写的程序,可能会遇到用C写的array,但C++的std::vector远比array好用,所以可能必须将array转成std::vector继续维护,以下的程序demo如何将array转成std::vector。
1
/*
2
(C) OOMusou 2006 http://oomusou.cnblogs.com
3
4
Filename : ArrayToVectorByConstructor.cpp
5
Compiler : Visual C++ 8.0
6
Description : Demo how to convert array to vector by vector constructor
7
Release : 11/15/2006 1.0
8
12/10/2006 2.0
9
*/
10
#include <iostream>
11
#include <vector>
12
#include <algorithm>
13
14
using namespace std;
15
16
int main() {
17
//const int iaSize = 11;
18
int ia[] = {0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89};
19
20
// We specify the address of the first element of
21
// the array as 1st argument, and the address of
22
// one past the last element as 2nd argument.
23
vector<int> ivec(ia,ia + sizeof(ia)/sizeof(int));
24
copy(ivec.begin(), ivec.end(), ostream_iterator<int>(cout, "\n"));
25
26
return 0;
27
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

See Also
(原創) 如何将array转成std::vector? (使用vector.insert) (C/C++) (STL)