-
May 1st, 2015, 01:42 AM
#1
accessing nested elements...
so lets assume i have a nested vector in a set or vice versa o in a more general form a container in a container
example...
Code:
std::set<vector<int> > my_tuple;
how do i access individual elements of lets say the first vector in the set!
Or the last element in the 3rd vector?
thanks
-
May 1st, 2015, 02:24 AM
#2
Re: accessing nested elements...
Suppose you have a std::set<int>. How would you access the first int in the set? You would do something similiar when the set contains vectors.
Now, suppose you have a vector. How would you access the last element in the vector? You would do something similiar for the 3rd vector in the set.
-
May 1st, 2015, 02:34 AM
#3
Re: accessing nested elements...
come on please, these type of replies are just frustrating !!!
we are not here playing guess games and stuff!
if you want to help you do so, giving suggestions plus an example!
I'VE TRIED AND AM NOT GETTING IT ... if you can't help with explanations and a valid example don't waste your time writing and don't waste my time too...
using the example in question... do i view my_tuple as a set or a vector? my_tuple[3] ?
how some of you guys behave as if you knew programming from birth mavels me...
IF YOU CAN'T HELP JUST ROLL ON, surely you have something better doing and not wasting time on forums!!! **** it!
-
May 1st, 2015, 02:56 AM
#4
Re: accessing nested elements...
Originally Posted by TheLionKing
if you want to help you do so, giving suggestions plus an example!
I did that, i.e., I suggested that you attempt with the simple containers first. What I did not do was to provide a code sample, but that was because I expected that my suggestions should be sufficient to get you started.
Originally Posted by TheLionKing
I'VE TRIED AND AM NOT GETTING IT ... if you can't help with explanations and a valid example don't waste your time writing and don't waste my time too...
You did not show any evidence of trying with my simplified versions, so how was I to know that you tried? If you cannot access the elements of a std::set<int> or a std::vector<int> then don't waste your time trying to access a std::set<std::vector<int>>.
Originally Posted by TheLionKing
using the example in question... do i view my_tuple as a set or a vector? my_tuple[3] ?
No, start with my suggested simplified versions first. Let's examine possible code for a std::set<int>:
Code:
std::set<int> x;
x.insert(123);
auto iter = x.find(123);
if (iter != x.end())
{
std::cout << *iter << std::endl;
}
else
{
std::cout << "Not found" << std::endl;
}
If you are compiling with respect to C++03 or earlier, substitute auto with std::set<int>::iterator.
Now, let's examine possible code for a std::vector<int>:
Code:
std::vector<int> y;
y.push_back(456);
std::cout << y[0] << std::endl;
So we see that for std::set, we might use the find member function to find the element with the given value in the set. For std::vector, assuming we know the index is valid, we might use the overloaded operator[]. Therefore, we are ready to tackle a std::set<std::vector<int>>:
Code:
std::vector<int> y;
y.push_back(456);
std::set<std::vector<int>> z;
z.insert(y);
auto iter = z.find(y);
if (iter != z.end())
{
std::cout << (*iter)[0] << std::endl;
}
else
{
std::cout << "Not found" << std::endl;
}
To answer your question directly then, you should view my_tuple as a set of vectors of int, because that is its type. Notice that the (*iter) syntax corresponds to the example for std::set<int> because z is a std::set and iter is a iterator to an element in z, then the [0] syntax corresponds to the example for std::vector<int>, because *iter is a std::vector<int>.
Originally Posted by TheLionKing
how some of you guys behave as if you knew programming from birth mavels me...
If I knew programming from birth, I would not need to simplify complex code to understand them piece by piece
Last edited by laserlight; May 1st, 2015 at 03:15 AM.
-
May 1st, 2015, 05:19 AM
#5
Re: accessing nested elements...
For readability, I sometimes do the following:
Code:
// given :
set<vector<int>> s;
Code:
for (auto it=s.begin(); it!=s.end(); ++it)
{
const vector<int> & v = *it; // a reference so a copy is not made
// use v
}
In a similar fashion, you can use a range based for loop if your compiler supports it:
Code:
for (const vector<int> & v : s) // a reference so a copy is not made
{
//use v
}
Last edited by Philip Nicoletti; May 1st, 2015 at 06:33 AM.
Reason: add range based for loop
-
May 1st, 2015, 10:25 AM
#6
Re: accessing nested elements...
OK thank you laserlight and sorry for my rant! but your solution was not the solution to my problem but thanks for your time
Thanks to you too Philip
Last edited by TheLionKing; May 1st, 2015 at 12:16 PM.
-
May 1st, 2015, 12:20 PM
#7
Re: accessing nested elements...
std::set< std::vector> > set_of_vectors ;
// ...
for( const auto& vec : set_of_vectors) // for each vector vec in set_of_vectors
{
for( int v : vec ) std::cout << v << ' ' ; // for each int v in the vector
std::cout << '\n' ;
i was after something like this...
-
May 1st, 2015, 01:08 PM
#8
Re: accessing nested elements...
Originally Posted by TheLionKing
i was after something like this...
Ah. It would have then been clearer if you asked how to iterate over the inner elements of a std::set<vector<int>> using range-based for loops.
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
|