-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvec_of_vecs2.cpp
More file actions
43 lines (34 loc) · 935 Bytes
/
Copy pathvec_of_vecs2.cpp
File metadata and controls
43 lines (34 loc) · 935 Bytes
1
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
Populate a vector of vectors of floats.
Traverse and print the values (cols) in each vector (rows).
*/
#include <iostream> // std::cout
#include <vector> // std::vector
using std::cout;
using std::endl;
int main() {
int rows = 5;
int cols[] = {10, 7, 11, 4, 1};
std::vector< std::vector<float> > vvec(rows);
for (int i=0; i<rows; i++) {
vvec[i] = std::vector<float>(cols[i]);
for (int j=0; j<cols[i]; j++) {
vvec[i][j] = j/((i+1)*10.0);
}
}
cout.precision(5);
cout.setf(std::ios::fixed, std::ios::floatfield);
// Iterate method 1
//for (int i=0; i<rows; i++) {
// for (int j=0; j<vvec[i].size(); j++) {
// cout << vvec[i][j] << " ";
// }
// Iterate method 2
for (int i=0; i<rows; i++) {
for (auto& val: vvec[i]) {
cout << val << " ";
}
cout << endl;
}
return 0;
}