Get row vector of 2d vector in C++What are the differences between a pointer variable and a reference variable in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListC++ STL Vectors: Get iterator from index?C++ Singleton design patternWhat is the “-->” operator in C++?Appending a vector to a vectorWhy are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
How to run a prison with the smallest amount of guards?
How does the UK government determine the size of a mandate?
What does the word "Atten" mean?
Do all network devices need to make routing decisions, regardless of communication across networks or within a network?
Type int? vs type int
How can a function with a hole (removable discontinuity) equal a function with no hole?
How easy is it to start Magic from scratch?
How do I find the solutions of the following equation?
Purchasing a ticket for someone else in another country?
Why escape if the_content isnt?
Customer Requests (Sometimes) Drive Me Bonkers!
Is exact Kanji stroke length important?
How do I extract a value from a time formatted value in excel?
How does Loki do this?
For a non-Jew, is there a punishment for not observing the 7 Noahide Laws?
System.debug(JSON.Serialize(o)) Not longer shows full string
Sort a list by elements of another list
Lay out the Carpet
How to Reset Passwords on Multiple Websites Easily?
Method to test if a number is a perfect power?
Is there a good way to store credentials outside of a password manager?
How long to clear the 'suck zone' of a turbofan after start is initiated?
Why Were Madagascar and New Zealand Discovered So Late?
Is a stroke of luck acceptable after a series of unfavorable events?
Get row vector of 2d vector in C++
What are the differences between a pointer variable and a reference variable in C++?How can I profile C++ code running on Linux?The Definitive C++ Book Guide and ListC++ STL Vectors: Get iterator from index?C++ Singleton design patternWhat is the “-->” operator in C++?Appending a vector to a vectorWhy are elementwise additions much faster in separate loops than in a combined loop?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionReplacing a 32-bit loop counter with 64-bit introduces crazy performance deviations
I have a vector of vector in C++ defined with: vector < vector<double> > A;
Let's suppose that A has been filled with some values. Is there a quick way to extract a row vector from A ?
For instance, A[0] will give me the first column vector, but how can I get quicky the first row vector?
c++ vector
add a comment |
I have a vector of vector in C++ defined with: vector < vector<double> > A;
Let's suppose that A has been filled with some values. Is there a quick way to extract a row vector from A ?
For instance, A[0] will give me the first column vector, but how can I get quicky the first row vector?
c++ vector
1
strictly speaking in yourvector<vector>>there is no row-vector. If you want one you need to construct it. Depending on what you want you should consider to have a view rather than creating a new container
– user463035818
Mar 7 at 12:50
I guess I'm misunderstanding things but wouldn't A[0] be the first row?
– c_student
Mar 7 at 12:55
1
@c_student its a matter of convention, swap "row" with "column" and the question is still the same
– user463035818
Mar 7 at 12:58
4
If this is supposed to be a matrix, you shouldn't usevector<vector<double>>(memory locality, allocations). Look at Eigen, it has all you need!
– Tobi
Mar 7 at 12:58
what did you try? why is it too slow? what to you need the row vector for? I am afraid without more context you cannot get a better answer than "create a new vector and fill it via looping over your matrix"
– user463035818
Mar 7 at 13:00
add a comment |
I have a vector of vector in C++ defined with: vector < vector<double> > A;
Let's suppose that A has been filled with some values. Is there a quick way to extract a row vector from A ?
For instance, A[0] will give me the first column vector, but how can I get quicky the first row vector?
c++ vector
I have a vector of vector in C++ defined with: vector < vector<double> > A;
Let's suppose that A has been filled with some values. Is there a quick way to extract a row vector from A ?
For instance, A[0] will give me the first column vector, but how can I get quicky the first row vector?
c++ vector
c++ vector
asked Mar 7 at 12:49
VictorVictor
9318
9318
1
strictly speaking in yourvector<vector>>there is no row-vector. If you want one you need to construct it. Depending on what you want you should consider to have a view rather than creating a new container
– user463035818
Mar 7 at 12:50
I guess I'm misunderstanding things but wouldn't A[0] be the first row?
– c_student
Mar 7 at 12:55
1
@c_student its a matter of convention, swap "row" with "column" and the question is still the same
– user463035818
Mar 7 at 12:58
4
If this is supposed to be a matrix, you shouldn't usevector<vector<double>>(memory locality, allocations). Look at Eigen, it has all you need!
– Tobi
Mar 7 at 12:58
what did you try? why is it too slow? what to you need the row vector for? I am afraid without more context you cannot get a better answer than "create a new vector and fill it via looping over your matrix"
– user463035818
Mar 7 at 13:00
add a comment |
1
strictly speaking in yourvector<vector>>there is no row-vector. If you want one you need to construct it. Depending on what you want you should consider to have a view rather than creating a new container
– user463035818
Mar 7 at 12:50
I guess I'm misunderstanding things but wouldn't A[0] be the first row?
– c_student
Mar 7 at 12:55
1
@c_student its a matter of convention, swap "row" with "column" and the question is still the same
– user463035818
Mar 7 at 12:58
4
If this is supposed to be a matrix, you shouldn't usevector<vector<double>>(memory locality, allocations). Look at Eigen, it has all you need!
– Tobi
Mar 7 at 12:58
what did you try? why is it too slow? what to you need the row vector for? I am afraid without more context you cannot get a better answer than "create a new vector and fill it via looping over your matrix"
– user463035818
Mar 7 at 13:00
1
1
strictly speaking in your
vector<vector>> there is no row-vector. If you want one you need to construct it. Depending on what you want you should consider to have a view rather than creating a new container– user463035818
Mar 7 at 12:50
strictly speaking in your
vector<vector>> there is no row-vector. If you want one you need to construct it. Depending on what you want you should consider to have a view rather than creating a new container– user463035818
Mar 7 at 12:50
I guess I'm misunderstanding things but wouldn't A[0] be the first row?
– c_student
Mar 7 at 12:55
I guess I'm misunderstanding things but wouldn't A[0] be the first row?
– c_student
Mar 7 at 12:55
1
1
@c_student its a matter of convention, swap "row" with "column" and the question is still the same
– user463035818
Mar 7 at 12:58
@c_student its a matter of convention, swap "row" with "column" and the question is still the same
– user463035818
Mar 7 at 12:58
4
4
If this is supposed to be a matrix, you shouldn't use
vector<vector<double>> (memory locality, allocations). Look at Eigen, it has all you need!– Tobi
Mar 7 at 12:58
If this is supposed to be a matrix, you shouldn't use
vector<vector<double>> (memory locality, allocations). Look at Eigen, it has all you need!– Tobi
Mar 7 at 12:58
what did you try? why is it too slow? what to you need the row vector for? I am afraid without more context you cannot get a better answer than "create a new vector and fill it via looping over your matrix"
– user463035818
Mar 7 at 13:00
what did you try? why is it too slow? what to you need the row vector for? I am afraid without more context you cannot get a better answer than "create a new vector and fill it via looping over your matrix"
– user463035818
Mar 7 at 13:00
add a comment |
3 Answers
3
active
oldest
votes
There is no "quick" way with that data structure, you have to iterate each column vector and get the value for desired row and add it to temporary row vector. Wether this is fast enough for you or not depends on what you need. To make it as fast as possible, be sure to allocate right amount of space in the target row vector, so it doesn't need to be resized while you add the values to it.
Simple solution to performance problem is to use some existing matrix library, such as Eigen suggested in comments.
If you need to do this yourself (because it is assignment, or because of licensing issues, or whatever), you should probably create your own "Matrix 2D" class, and hide implementation details in it. Then depending on what exactly you need, you can employ tricks like:
- have a "cache" for rows, so if same row is fetched many times, it can be fetched from the cache and a new vector does not need to be created
- store data both as vector of row vectors, and vector of column vectors, so you can get either rows or columns at constant time, at the cost of using more memory and making changes twice as expensive due to duplication of data
- dynamically change the internal representation according to current needs, so you get the fixed memory usage, but need to pay the processing cost when you need to change the internal representation
- store data in flat vector with size of rows*columns, and calculate the correct offset in your own code from row and column
But it bears repeating: someone has already done this for you, so try to use an existing library, if you can...
add a comment |
There is no really fast way to do that. Also as pointed out, I would say that the convention is the other way around, meaning that However even trying to get a column is not really trivial, sinceA[0] is actually the first row, rather than the first column.
0, 1, 2, 3, 4
0
0, 1, 2
is a very possible vector<vector<double>> A, but there is no real column 1, 2, 3 or 4. If you wish to enforce behavior like same length columns, creating a Matrix class may be a good idea (or using a library).
You could write a function that would return a vector<double> by iterating over the rows, storing the appropriate column value. But you would have to be careful about whether you want to copy or point to the matrix values (vector<double> / vector<double *>). This is not very fast as the values are not next to each other in memory.
1
I wouldn't say there is any kind of "convention" over rows and columns. That's completely arbitrary decision which is which. One way is probably more common, but it's not a convention to follow.
– hyde
Mar 7 at 13:40
Good point. I guess I would just like to believe that my way is the way everyone does it :D. If there is no performance aspect involved, because when I write it as a literal usingover multiple lines, you get that nice row-wise schema.
– Raven221221221
Mar 7 at 13:53
add a comment |
The answer is: in your case there is no corresponding simple option as for columns. And one of the reasons is that vector> is a particular poorly suited container for multi-dimensional data.
In multi-dimension it is one of the important design decisions: which dimension you want to access most efficiently, and based on the answer you can define your container (which might be very specialized and complex).
For example in your case: it is entirely up to you to call A[0] a 'column' or a 'row'. You only need to do it consistently (best define a small interface around which makes this explicit). But STOP, don't do that:
This brings you to the next level: for multi-dimensional data you would typically not use vector> (but this is a different issue). Look at smart and efficient solutions already existing e.g. in ublas https://www.boost.org/doc/libs/1_65_1/libs/numeric/ublas/doc/index.html or eigen3 https://eigen.tuxfamily.org/dox/
You will never be able to beat these highly optimized libraries.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044194%2fget-row-vector-of-2d-vector-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There is no "quick" way with that data structure, you have to iterate each column vector and get the value for desired row and add it to temporary row vector. Wether this is fast enough for you or not depends on what you need. To make it as fast as possible, be sure to allocate right amount of space in the target row vector, so it doesn't need to be resized while you add the values to it.
Simple solution to performance problem is to use some existing matrix library, such as Eigen suggested in comments.
If you need to do this yourself (because it is assignment, or because of licensing issues, or whatever), you should probably create your own "Matrix 2D" class, and hide implementation details in it. Then depending on what exactly you need, you can employ tricks like:
- have a "cache" for rows, so if same row is fetched many times, it can be fetched from the cache and a new vector does not need to be created
- store data both as vector of row vectors, and vector of column vectors, so you can get either rows or columns at constant time, at the cost of using more memory and making changes twice as expensive due to duplication of data
- dynamically change the internal representation according to current needs, so you get the fixed memory usage, but need to pay the processing cost when you need to change the internal representation
- store data in flat vector with size of rows*columns, and calculate the correct offset in your own code from row and column
But it bears repeating: someone has already done this for you, so try to use an existing library, if you can...
add a comment |
There is no "quick" way with that data structure, you have to iterate each column vector and get the value for desired row and add it to temporary row vector. Wether this is fast enough for you or not depends on what you need. To make it as fast as possible, be sure to allocate right amount of space in the target row vector, so it doesn't need to be resized while you add the values to it.
Simple solution to performance problem is to use some existing matrix library, such as Eigen suggested in comments.
If you need to do this yourself (because it is assignment, or because of licensing issues, or whatever), you should probably create your own "Matrix 2D" class, and hide implementation details in it. Then depending on what exactly you need, you can employ tricks like:
- have a "cache" for rows, so if same row is fetched many times, it can be fetched from the cache and a new vector does not need to be created
- store data both as vector of row vectors, and vector of column vectors, so you can get either rows or columns at constant time, at the cost of using more memory and making changes twice as expensive due to duplication of data
- dynamically change the internal representation according to current needs, so you get the fixed memory usage, but need to pay the processing cost when you need to change the internal representation
- store data in flat vector with size of rows*columns, and calculate the correct offset in your own code from row and column
But it bears repeating: someone has already done this for you, so try to use an existing library, if you can...
add a comment |
There is no "quick" way with that data structure, you have to iterate each column vector and get the value for desired row and add it to temporary row vector. Wether this is fast enough for you or not depends on what you need. To make it as fast as possible, be sure to allocate right amount of space in the target row vector, so it doesn't need to be resized while you add the values to it.
Simple solution to performance problem is to use some existing matrix library, such as Eigen suggested in comments.
If you need to do this yourself (because it is assignment, or because of licensing issues, or whatever), you should probably create your own "Matrix 2D" class, and hide implementation details in it. Then depending on what exactly you need, you can employ tricks like:
- have a "cache" for rows, so if same row is fetched many times, it can be fetched from the cache and a new vector does not need to be created
- store data both as vector of row vectors, and vector of column vectors, so you can get either rows or columns at constant time, at the cost of using more memory and making changes twice as expensive due to duplication of data
- dynamically change the internal representation according to current needs, so you get the fixed memory usage, but need to pay the processing cost when you need to change the internal representation
- store data in flat vector with size of rows*columns, and calculate the correct offset in your own code from row and column
But it bears repeating: someone has already done this for you, so try to use an existing library, if you can...
There is no "quick" way with that data structure, you have to iterate each column vector and get the value for desired row and add it to temporary row vector. Wether this is fast enough for you or not depends on what you need. To make it as fast as possible, be sure to allocate right amount of space in the target row vector, so it doesn't need to be resized while you add the values to it.
Simple solution to performance problem is to use some existing matrix library, such as Eigen suggested in comments.
If you need to do this yourself (because it is assignment, or because of licensing issues, or whatever), you should probably create your own "Matrix 2D" class, and hide implementation details in it. Then depending on what exactly you need, you can employ tricks like:
- have a "cache" for rows, so if same row is fetched many times, it can be fetched from the cache and a new vector does not need to be created
- store data both as vector of row vectors, and vector of column vectors, so you can get either rows or columns at constant time, at the cost of using more memory and making changes twice as expensive due to duplication of data
- dynamically change the internal representation according to current needs, so you get the fixed memory usage, but need to pay the processing cost when you need to change the internal representation
- store data in flat vector with size of rows*columns, and calculate the correct offset in your own code from row and column
But it bears repeating: someone has already done this for you, so try to use an existing library, if you can...
answered Mar 7 at 13:11
hydehyde
32k1589133
32k1589133
add a comment |
add a comment |
There is no really fast way to do that. Also as pointed out, I would say that the convention is the other way around, meaning that However even trying to get a column is not really trivial, sinceA[0] is actually the first row, rather than the first column.
0, 1, 2, 3, 4
0
0, 1, 2
is a very possible vector<vector<double>> A, but there is no real column 1, 2, 3 or 4. If you wish to enforce behavior like same length columns, creating a Matrix class may be a good idea (or using a library).
You could write a function that would return a vector<double> by iterating over the rows, storing the appropriate column value. But you would have to be careful about whether you want to copy or point to the matrix values (vector<double> / vector<double *>). This is not very fast as the values are not next to each other in memory.
1
I wouldn't say there is any kind of "convention" over rows and columns. That's completely arbitrary decision which is which. One way is probably more common, but it's not a convention to follow.
– hyde
Mar 7 at 13:40
Good point. I guess I would just like to believe that my way is the way everyone does it :D. If there is no performance aspect involved, because when I write it as a literal usingover multiple lines, you get that nice row-wise schema.
– Raven221221221
Mar 7 at 13:53
add a comment |
There is no really fast way to do that. Also as pointed out, I would say that the convention is the other way around, meaning that However even trying to get a column is not really trivial, sinceA[0] is actually the first row, rather than the first column.
0, 1, 2, 3, 4
0
0, 1, 2
is a very possible vector<vector<double>> A, but there is no real column 1, 2, 3 or 4. If you wish to enforce behavior like same length columns, creating a Matrix class may be a good idea (or using a library).
You could write a function that would return a vector<double> by iterating over the rows, storing the appropriate column value. But you would have to be careful about whether you want to copy or point to the matrix values (vector<double> / vector<double *>). This is not very fast as the values are not next to each other in memory.
1
I wouldn't say there is any kind of "convention" over rows and columns. That's completely arbitrary decision which is which. One way is probably more common, but it's not a convention to follow.
– hyde
Mar 7 at 13:40
Good point. I guess I would just like to believe that my way is the way everyone does it :D. If there is no performance aspect involved, because when I write it as a literal usingover multiple lines, you get that nice row-wise schema.
– Raven221221221
Mar 7 at 13:53
add a comment |
There is no really fast way to do that. Also as pointed out, I would say that the convention is the other way around, meaning that However even trying to get a column is not really trivial, sinceA[0] is actually the first row, rather than the first column.
0, 1, 2, 3, 4
0
0, 1, 2
is a very possible vector<vector<double>> A, but there is no real column 1, 2, 3 or 4. If you wish to enforce behavior like same length columns, creating a Matrix class may be a good idea (or using a library).
You could write a function that would return a vector<double> by iterating over the rows, storing the appropriate column value. But you would have to be careful about whether you want to copy or point to the matrix values (vector<double> / vector<double *>). This is not very fast as the values are not next to each other in memory.
There is no really fast way to do that. Also as pointed out, I would say that the convention is the other way around, meaning that However even trying to get a column is not really trivial, sinceA[0] is actually the first row, rather than the first column.
0, 1, 2, 3, 4
0
0, 1, 2
is a very possible vector<vector<double>> A, but there is no real column 1, 2, 3 or 4. If you wish to enforce behavior like same length columns, creating a Matrix class may be a good idea (or using a library).
You could write a function that would return a vector<double> by iterating over the rows, storing the appropriate column value. But you would have to be careful about whether you want to copy or point to the matrix values (vector<double> / vector<double *>). This is not very fast as the values are not next to each other in memory.
edited Mar 7 at 13:55
answered Mar 7 at 13:29
Raven221221221Raven221221221
1017
1017
1
I wouldn't say there is any kind of "convention" over rows and columns. That's completely arbitrary decision which is which. One way is probably more common, but it's not a convention to follow.
– hyde
Mar 7 at 13:40
Good point. I guess I would just like to believe that my way is the way everyone does it :D. If there is no performance aspect involved, because when I write it as a literal usingover multiple lines, you get that nice row-wise schema.
– Raven221221221
Mar 7 at 13:53
add a comment |
1
I wouldn't say there is any kind of "convention" over rows and columns. That's completely arbitrary decision which is which. One way is probably more common, but it's not a convention to follow.
– hyde
Mar 7 at 13:40
Good point. I guess I would just like to believe that my way is the way everyone does it :D. If there is no performance aspect involved, because when I write it as a literal usingover multiple lines, you get that nice row-wise schema.
– Raven221221221
Mar 7 at 13:53
1
1
I wouldn't say there is any kind of "convention" over rows and columns. That's completely arbitrary decision which is which. One way is probably more common, but it's not a convention to follow.
– hyde
Mar 7 at 13:40
I wouldn't say there is any kind of "convention" over rows and columns. That's completely arbitrary decision which is which. One way is probably more common, but it's not a convention to follow.
– hyde
Mar 7 at 13:40
Good point. I guess I would just like to believe that my way is the way everyone does it :D. If there is no performance aspect involved, because when I write it as a literal using
over multiple lines, you get that nice row-wise schema.– Raven221221221
Mar 7 at 13:53
Good point. I guess I would just like to believe that my way is the way everyone does it :D. If there is no performance aspect involved, because when I write it as a literal using
over multiple lines, you get that nice row-wise schema.– Raven221221221
Mar 7 at 13:53
add a comment |
The answer is: in your case there is no corresponding simple option as for columns. And one of the reasons is that vector> is a particular poorly suited container for multi-dimensional data.
In multi-dimension it is one of the important design decisions: which dimension you want to access most efficiently, and based on the answer you can define your container (which might be very specialized and complex).
For example in your case: it is entirely up to you to call A[0] a 'column' or a 'row'. You only need to do it consistently (best define a small interface around which makes this explicit). But STOP, don't do that:
This brings you to the next level: for multi-dimensional data you would typically not use vector> (but this is a different issue). Look at smart and efficient solutions already existing e.g. in ublas https://www.boost.org/doc/libs/1_65_1/libs/numeric/ublas/doc/index.html or eigen3 https://eigen.tuxfamily.org/dox/
You will never be able to beat these highly optimized libraries.
add a comment |
The answer is: in your case there is no corresponding simple option as for columns. And one of the reasons is that vector> is a particular poorly suited container for multi-dimensional data.
In multi-dimension it is one of the important design decisions: which dimension you want to access most efficiently, and based on the answer you can define your container (which might be very specialized and complex).
For example in your case: it is entirely up to you to call A[0] a 'column' or a 'row'. You only need to do it consistently (best define a small interface around which makes this explicit). But STOP, don't do that:
This brings you to the next level: for multi-dimensional data you would typically not use vector> (but this is a different issue). Look at smart and efficient solutions already existing e.g. in ublas https://www.boost.org/doc/libs/1_65_1/libs/numeric/ublas/doc/index.html or eigen3 https://eigen.tuxfamily.org/dox/
You will never be able to beat these highly optimized libraries.
add a comment |
The answer is: in your case there is no corresponding simple option as for columns. And one of the reasons is that vector> is a particular poorly suited container for multi-dimensional data.
In multi-dimension it is one of the important design decisions: which dimension you want to access most efficiently, and based on the answer you can define your container (which might be very specialized and complex).
For example in your case: it is entirely up to you to call A[0] a 'column' or a 'row'. You only need to do it consistently (best define a small interface around which makes this explicit). But STOP, don't do that:
This brings you to the next level: for multi-dimensional data you would typically not use vector> (but this is a different issue). Look at smart and efficient solutions already existing e.g. in ublas https://www.boost.org/doc/libs/1_65_1/libs/numeric/ublas/doc/index.html or eigen3 https://eigen.tuxfamily.org/dox/
You will never be able to beat these highly optimized libraries.
The answer is: in your case there is no corresponding simple option as for columns. And one of the reasons is that vector> is a particular poorly suited container for multi-dimensional data.
In multi-dimension it is one of the important design decisions: which dimension you want to access most efficiently, and based on the answer you can define your container (which might be very specialized and complex).
For example in your case: it is entirely up to you to call A[0] a 'column' or a 'row'. You only need to do it consistently (best define a small interface around which makes this explicit). But STOP, don't do that:
This brings you to the next level: for multi-dimensional data you would typically not use vector> (but this is a different issue). Look at smart and efficient solutions already existing e.g. in ublas https://www.boost.org/doc/libs/1_65_1/libs/numeric/ublas/doc/index.html or eigen3 https://eigen.tuxfamily.org/dox/
You will never be able to beat these highly optimized libraries.
answered Mar 7 at 13:18
Ralf UlrichRalf Ulrich
15417
15417
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55044194%2fget-row-vector-of-2d-vector-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
strictly speaking in your
vector<vector>>there is no row-vector. If you want one you need to construct it. Depending on what you want you should consider to have a view rather than creating a new container– user463035818
Mar 7 at 12:50
I guess I'm misunderstanding things but wouldn't A[0] be the first row?
– c_student
Mar 7 at 12:55
1
@c_student its a matter of convention, swap "row" with "column" and the question is still the same
– user463035818
Mar 7 at 12:58
4
If this is supposed to be a matrix, you shouldn't use
vector<vector<double>>(memory locality, allocations). Look at Eigen, it has all you need!– Tobi
Mar 7 at 12:58
what did you try? why is it too slow? what to you need the row vector for? I am afraid without more context you cannot get a better answer than "create a new vector and fill it via looping over your matrix"
– user463035818
Mar 7 at 13:00