|
-
February 4th, 2016, 03:31 AM
#1
Divide 1d vector into 2d vector
Hi i need divide below vector by 3 into a 2d vector
Code:
vector<int> a = {5,6,8,7,5,4,7,5};
Example
Code:
vector<vector<int> > b = {{5,6,8},{7,5,4},{7,5}};
How can i do that i need help
-
February 4th, 2016, 03:55 AM
#2
Re: Divide 1d vector into 2d vector
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.
-
February 4th, 2016, 04:09 AM
#3
Re: Divide 1d vector into 2d vector
Oky i did that using for loop is there any other way to do this more essay and fast way
-
February 4th, 2016, 04:27 AM
#4
Re: Divide 1d vector into 2d vector
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.
-
February 4th, 2016, 05:03 AM
#5
Re: Divide 1d vector into 2d vector
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 ;
}
Last edited by Ramees219; February 4th, 2016 at 05:24 AM.
-
February 4th, 2016, 05:46 AM
#6
Re: Divide 1d vector into 2d vector
From your OP, one way of obtaining the partition could be
Code:
#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;
}
}
NOTE but see posts #10 and #12
Last edited by 2kaud; February 4th, 2016 at 07:29 AM.
Reason: Note
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 4th, 2016, 05:55 AM
#7
Re: Divide 1d vector into 2d vector
constexpr size_t part_size = 3U;
I didn't understand this line . 3U how its a size what is constexpr
-
February 4th, 2016, 06:08 AM
#8
Re: Divide 1d vector into 2d vector
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
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;
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;
}
NOTE but see posts #10 and #12
Last edited by 2kaud; February 4th, 2016 at 07:30 AM.
Reason: Note
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 4th, 2016, 06:10 AM
#9
Re: Divide 1d vector into 2d vector
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
February 4th, 2016, 06:33 AM
#10
Re: Divide 1d vector into 2d vector
 Originally Posted by 2kaud
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
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;
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;
}
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
Last edited by Ramees219; February 4th, 2016 at 06:36 AM.
-
February 4th, 2016, 06:44 AM
#11
Re: Divide 1d vector into 2d vector
Works OK for me in compile configuration release mode with VS 2015.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 4th, 2016, 07:28 AM
#12
Re: Divide 1d vector into 2d vector
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 . Revised code for this is
Code:
#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;
}
and
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;
}
Last edited by 2kaud; February 4th, 2016 at 07:30 AM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 4th, 2016, 07:36 AM
#13
Re: Divide 1d vector into 2d vector
Ok thanks for the help . I will check the code soon . Installing vs 2015
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|