Hi i need divide below vector by 3 into a 2d vector
ExampleCode:vector<int> a = {5,6,8,7,5,4,7,5};
How can i do that i need helpCode:vector<vector<int> > b = {{5,6,8},{7,5,4},{7,5}};
Printable View
Hi i need divide below vector by 3 into a 2d vector
ExampleCode:vector<int> a = {5,6,8,7,5,4,7,5};
How can i do that i need helpCode:vector<vector<int> > b = {{5,6,8},{7,5,4},{7,5}};
Have you ever done that exercise where you are given a sequence of values, and you're supposed to display them in rows of say, 3 values per row? You can adapt that idea here.
Oky i did that using for loop is there any other way to do this more essay and fast way :)
So you managed to implement the code for your original problem using a for loop? I think that's fine in terms of how easy it is to understand and how fast it will be.
please review my code i need to make it perfect . show me if any better way
and how can i convert a vector<int> a = {4,5,8,7,4,5,4,1,5,5}; value to a long long int variable
Code:#include <iostream>
#include <vector>
using namespace std;
vector<int> Lint_Mply(vector<int> , vector<int> );
int main(){
vector<int> input1 = { 4, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 5, 4, 5, 5, 2, 2, 6, 5, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
vector<int> input2 = { 8, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 5, 4, 5, 5, 2, 2, 6, 5, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
//vector<int> input1 = { 4, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 6, 5, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
//vector<int> input2 = { 8, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 5, 4, 5, 5, 2, 2, 6, 5, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
//vector<int> input1 = { 4, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 5, 4, 5, 5, 2, 2, 6, 5, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
//vector<int> input2 = { 8, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 4, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
Lint_Mply(input1,input2);
return 0;
}
vector<int>Lint_Mply(vector<int> in1 , vector<int> in2){
vector<int> out = { 0 };
vector<vector<int>> Sv_int1;
vector<vector<int>> Sv_int2;
// divide inputs here
int sz1 = in1.size();
int sz2 = in2.size();
int p = 0;
int c = 0;
vector<int>a;
Sv_int1.push_back(a);
for (int i = 0; i < sz1; i++ ){
p++;
Sv_int1[c].push_back(in1[i]);
if (p == 9){
Sv_int1.push_back(a);
p = 0;
c++;
}
}
p = 0;
c = 0;
vector<int>b;
Sv_int2.push_back(b);
for (int i = 0; i < sz2; i++){
p++;
Sv_int2[c].push_back(in2[i]);
if (p == 9){
Sv_int2.push_back(b);
p = 0;
c++;
}
}
// segment multiplicaion
return out ;
}
From your OP, one way of obtaining the partition could be
NOTE but see posts #10 and #12Code:#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cmath>
using namespace std;
int main()
{
constexpr size_t part_size = 3U;
const vector<int> a = { 5, 6, 8, 7, 5, 4, 7, 5 };
vector<vector<int>> b (size_t(ceil(a.size() / double(part_size))));
//Partition
size_t vb = 0;
for (auto i = a.cbegin(); i < a.cend(); i += part_size, ++vb)
b[vb].assign(i, i + (((i + part_size) < a.cend()) ? part_size : distance(i, a.cend())));
//Display resulting vector
for (const auto& i1 : b) {
copy(i1.begin(), i1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
}
constexpr size_t part_size = 3U;
I didn't understand this line . 3U how its a size what is constexpr
3U means 3 of type unsigned - U for unsigned.
What compiler are you using? You may need to replace constexpr with const. Basically constexpr means a const expression that can be evaluated at compile time rather than run time.
Further consider
NOTE but see posts #10 and #12Code:#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iterator>
using namespace std;
vector<int> Lint_Mply(const vector<int>& in1, const vector<int>& in2);
vector<vector<int>> vpart(const vector<int>& vi);
int main() {
const vector<int> input1 = { 4, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 5, 4, 5, 5, 2, 2, 6, 5, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
const vector<int> input2 = { 8, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 5, 4, 5, 5, 2, 2, 6, 5, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
Lint_Mply(input1, input2);
return 0;
}
void vdisp(const vector<vector<int>>& vii)
{
for (const auto& i1 : vii) {
copy(i1.begin(), i1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
}
vector<vector<int>> vpart(const vector<int>& vi)
{
constexpr size_t part_size = 3U;
vector<vector<int>> b(size_t(ceil(vi.size() / double(part_size))));
size_t vb = 0;
for (auto i = vi.cbegin(); i < vi.cend(); i += part_size, ++vb)
b[vb].assign(i, i + (((i + part_size) < vi.cend()) ? part_size : distance(i, vi.cend())));
return b;
}
vector<int>Lint_Mply(const vector<int>& in1, const vector<int>& in2)
{
vector<int> out = { 0 };
vector<vector<int>> Sv_int1 (vpart(in1));
vector<vector<int>> Sv_int2 (vpart(in2));
vdisp(Sv_int1);
vdisp(Sv_int2);
// segment multiplication
return out;
}
consexpr is a way of saying that the variable is declared "const", so that no-one may change it. "constexpr" further means the variable's value is known at compile time, allowing the compiler to use it during compilation.
"3U" is just an unsigned integer literal. "3" would have worked too, but the "U" adds a bit more context for the reader.
I am using 2013 visual studio the first time i run constexpr error came in so i changed constexpr inside the vpart function to const then i run
Debug assertion fail vector iterator offset out of range error
If i run this in 2015 visual studio did i get same error
and how can i convert a vector<int> a = {4,5,8,7,4,5,4,1,5,5}; value to a long long int variable
Works OK for me in compile configuration release mode with VS 2015.
The observed issue happens in configuration debug mode because the iterator is incremented beyond the end of the vector before tests are done for this situation. This is fine for a vector in release mode but not in debug mode :rolleyes:. Revised code for this is
andCode:#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <cmath>
using namespace std;
int main()
{
constexpr size_t part_size = 3U;
const vector<int> a = { 5, 6, 8, 7, 5, 4, 7, 5 };
vector<vector<int>> b (size_t(ceil(a.size() / double(part_size))));
//Partition
size_t vb = 0;
size_t inc = 0;
for (auto i = a.cbegin(); i != a.cend(); i += inc, ++vb)
b[vb].assign(i, i + (inc = min(size_t(distance(i, a.cend())), part_size)));
//Display resulting vector
for (const auto& i1 : b) {
copy(i1.begin(), i1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
return 0;
}
Code:#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iterator>
using namespace std;
vector<int> Lint_Mply(const vector<int>& in1, const vector<int>& in2);
vector<vector<int>> vpart(const vector<int>& vi);
int main() {
const vector<int> input1 = { 4, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 5, 4, 5, 5, 2, 2, 6, 5, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
const vector<int> input2 = { 8, 2, 5, 6, 8, 5, 4, 2, 5, 5, 1, 2, 5, 4, 5, 5, 2, 2, 6, 5, 4, 1, 2, 5, 5, 5, 2, 2, 1, 5, 5 };
Lint_Mply(input1, input2);
return 0;
}
void vdisp(const vector<vector<int>>& vii)
{
for (const auto& i1 : vii) {
copy(i1.begin(), i1.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
}
vector<vector<int>> vpart(const vector<int>& vi)
{
constexpr size_t part_size = 3U;
vector<vector<int>> b(size_t(ceil(vi.size() / double(part_size))));
size_t vb = 0;
size_t inc = 0;
for (auto i = vi.cbegin(); i != vi.cend(); i += inc, ++vb)
b[vb].assign(i, i + (inc = min(size_t(distance(i, vi.cend())), part_size)));
return b;
}
vector<int>Lint_Mply(const vector<int>& in1, const vector<int>& in2)
{
vector<int> out = { 0 };
vector<vector<int>> Sv_int1 (vpart(in1));
vector<vector<int>> Sv_int2 (vpart(in2));
vdisp(Sv_int1);
vdisp(Sv_int2);
// segment multiplication
return out;
}
Ok thanks for the help . I will check the code soon . Installing vs 2015 :)