问题
I am trying to assign and print vector<vector<int>>
dynamically , However i cannot figure out how to do this i am stuck here is my program
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class A {
public:
vector<int> getVector(int s) {
vector <int> A(s);
for (int j = 0; j < s; j++) {
A.push_back(j);
}
return A;
}
};
int main() {
A obj;
int n = 5;
vector<vector<int>> A;
A.push_back(obj.getVector(n)); // pushes a vector on vector A
A.push_back(obj.getVector(n - 1));
vector<vector<int>> ::iterator it;
it = A.begin();
for (it; it != A.end(); it++) {
cout << *it;
}
return 0;
}
回答1:
vector<vector<int>> ::iterator it;
it = A.begin();
for (it; it != A.end(); it++) {
for(vector<int>::iterator it2 = it->begin(); it2 != it->end(); ++it2)
cout << *it2;
}
回答2:
Use range loop like
for( const auto &i : A ) // for elements in A, i is vector<int>
{
for( const auto &j : i ) // for sub elements in A, j is int
{
std::cout<<j;
}
}
or using iterator
for( vector<vector<int>>::iterator i = A.begin() ; i != A.end(); i++ )
{
for( vector<int>::iterator j = i->begin(); j != i->end(); j++ )
{
std::cout<<*j;
}
}
Thanks.
回答3:
Consider vector<vector> like a dynamic 2D array.
You will require 2 loops for this.
Also, you need to change a few things:
vector <int> A(s);
will initialise a vector of sizes
with all elements set to0
and then you are pushing the firstn
elements intoA
. So, changedvector <int> A(s);
tovector <int> A;
.using namespace std
is not considered as a good practice. Find out why.
Have a look at the following implementation:
#include<iostream>
#include<vector>
#include<string>
class A {
public:
std::vector<int> getVector(int s) {
std::vector <int> A; //Change 1
for (int j = 0; j < s; j++) {
A.push_back(j);
}
return A;
}
};
int main() {
A obj;
int n = 5;
std::vector<std::vector<int>> test_vector;
test_vector.push_back(obj.getVector(n)); // pushes a vector on vector A
test_vector.push_back(obj.getVector(n - 1));
std::vector<std::vector<int>> :: iterator test_vector_iterator;
test_vector_iterator = test_vector.begin();
for (test_vector_iterator; test_vector_iterator != test_vector.end(); test_vector_iterator++) {
std::vector<int> :: iterator inner_vector_iterator = (*test_vector_iterator).begin();
for(inner_vector_iterator; inner_vector_iterator != (*test_vector_iterator).end(); inner_vector_iterator++){
std::cout << *inner_vector_iterator <<" ";
}
std::cout << std::endl;
}
return 0;
}
Output:
0 1 2 3 4
0 1 2 3
回答4:
Nested std::for_each could do the trick
Applies the given function object f to the result of dereferencing every iterator in the range [first, last), in order.
We will make f
take std::vector<int>
as argument, and print it.
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<std::vector<int>> A{ {1,2}, {3, 4} };
std::for_each(A.begin(), A.end(), [](const auto& v) {
std::for_each(v.begin(), v.end(), [](auto const& it) {
std::cout << it << std::endl;
});
});
return 0;
}
Output
1
2
3
4
回答5:
Vector of vectors is an analog of 2d array. There is no standard method to serialize vector, a nested loop will do.
#include<iostream>
#include<vector>
#include<string>
#include <algorithm>
using std::for_each;
using std::cout;
using std::vector;
using std::endl;
class A {
public:
vector<int> getVector(int s) {
vector <int> A(s);
for (int j = 0; j < s; j++) {
A.push_back(j);
}
return A;
}
};
int main() {
A obj;
int n = 5;
vector<vector<int>> A;
A.push_back(obj.getVector(n)); // pushes a vector on vector A
A.push_back(obj.getVector(n - 1));
// classic for loop
for (auto itV = A.begin(), itVE = A.end(); itV != itVE; itV++)
{
for (auto itI = itV->begin(), itIE = itV->end(); itI != itIE; itI++)
{
cout << *itI;
}
}
cout << endl;
// much simpler range-based loop
for (auto& rowV : A ) // note that this a reference
// - no copy of stored vector is made.
for (auto el : rowV)
cout << el;
cout << endl;
// a generic lambda to serialize vector
auto print_vector = [](const auto& v) {
std::for_each(v.begin(), v.end(), [](auto const& it) {
std::cout << it << std::endl;
});
};
std::for_each(A.begin(), A.end(), print_vector );
return 0;
}
There are several ways to do that: use classic for() which is quite mouthful but allows fie control over some aspects, a range-based for loop is the shortest and most concise variant. THird approach is use of idiomatic function from standard library like for_each
, which would require creation of callable, which can be preferred if callable can be re-used of be swapped with something else, allowing some flexibility.
来源:https://stackoverflow.com/questions/63754495/how-to-print-vector-vectorint-in-c