Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...
Regards 11 5930
"kkirtac" <ka**********@gmail.comwrote in message
news:11**********************@y42g2000hsy.googlegr oups.com...
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...
Then why don't you use a vector? Generally if you want to randomly access
elements in a collection (by index) rather than sequentially (from begin()
to end()) you'll need a vector. Or you'll risk the added overhead of
traversing the whole list to get to the specific index you want.
-
Abdo Haji-Ali
Programmer
In|Framez
kkirtac a écrit :
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...
Use the std::advance algorithm:
list<T>::iterator it=list.begin();
int n=42;
std::advance(it,n);
//it is 42th element or list.end()
Michael
Michael DOUBEZ a écrit :
kkirtac a écrit :
>Hello, i want to reach a specified index in a list, as we can achieve this by the "[]" operator in a std::vector. I couldnt find a way to do it without iterators, stepping one by one..i need to point to a specified index and retrieve the value and then remove the value of the entry...
Use the std::advance algorithm:
list<T>::iterator it=list.begin();
int n=42;
std::advance(it,n);
//it is 42th element or list.end()
oups it is the 43th :)
>
Michael
kkirtac wrote:
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..i need to point to a
specified index and retrieve the value and then remove the value of
the entry...
template<class C>
typename C::value_type& getAtIndex(C &L, size_t i)
{
if (i >= L.size())
throw "Out of bounds";
typename C::iterator it = L.begin();
std::advance(it, i);
return *it;
}
...
list<intblah;
...
int& ref = getAtIndex(blah, 42);
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Aug 28, 5:10 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
template<class C>
typename C::value_type& getAtIndex(C &L, size_t i)
{
if (i >= L.size())
throw "Out of bounds";
typename C::iterator it = L.begin();
std::advance(it, i);
return *it;
}
...
list<intblah;
...
int& ref = getAtIndex(blah, 42);
I was wondering: why do you need `typename C::' instead of `C::' in
both of the cases? Also how does the compiler know what value_type is?
thanks,
Greg
greg wrote:
On Aug 28, 5:10 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
> template<class C> typename C::value_type& getAtIndex(C &L, size_t i) { if (i >= L.size()) throw "Out of bounds"; typename C::iterator it = L.begin(); std::advance(it, i); return *it; }
... list<intblah; ... int& ref = getAtIndex(blah, 42);
I was wondering: why do you need `typename C::' instead of `C::' in
both of the cases? Also how does the compiler know what value_type is?
FAQ, section 35. And by the time this function is instantiated, the
'C::value_type' is known because 'C' is something concrete.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Aug 28, 2:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
FAQ, section 35.
Doesn't say anything abut this issue?
greg wrote:
On Aug 28, 2:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>> FAQ, section 35.
Doesn't say anything abut this issue?
Huh? 35.18 does. It explains why 'typename' is needed, as I see it.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
On Aug 28, 3:23 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
greg wrote:
On Aug 28, 2:57 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
FAQ, section 35.
Doesn't say anything abut this issue?
Huh? 35.18 does. It explains why 'typename' is needed, as I see it.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
My bad.
On Aug 29, 9:44 am, greg <greg.johnse...@gmail.comwrote:
On Aug 28, 5:10 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
template<class C>
typename C::value_type& getAtIndex(C &L, size_t i)
{
if (i >= L.size())
throw "Out of bounds";
typename C::iterator it = L.begin();
std::advance(it, i);
return *it;
}
I was wondering: why do you need `typename C::' instead of `C::' in
both of the cases? Also how does the compiler know what value_type is?
The 'typename' keyword tells the compiler that C::value_type
is the name of a type (as opposed to the name of a variable),
so it can parse the source file correctly.
In this particular code it's not ambiguous, but in other
code it is; does:
C::foo & x;
declare a reference, or perform a bitwise AND ?
kkirtac wrote:
Hello, i want to reach a specified index in a list, as we can achieve
this by the "[]" operator in a std::vector. I couldnt find a way to do
it without iterators, stepping one by one..
There isn't. advance() has been suggested, but it just does exactly
that. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mike Pemberton |
last post by:
I'm sure there's a good explanation for this effect, but I get rather
a strange output from this little test:
#include <iostream>
#include <list>
int main()
{
std::list<int> int_list;
|
by: JustSomeGuy |
last post by:
I need to write an new class derived from the list class.
This class stores data in the list to the disk if an object
that is added to the list is over 1K in size.
What methods of the std stl...
|
by: Eric Lilja |
last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope
it's readable at least):
#include <algorithm>
#include <iostream>
#include <vector>
class Row
{
public:
|
by: PengYu.UT |
last post by:
Hi,
Suppose I have a list which contains pointers. I want the pointer got
by dereferencing the iterator be a pointer pointing to a const object.
But std::list<const T*>::const_iterator doens't...
|
by: Josh Mcfarlane |
last post by:
Just out of curiosity:
When would using std::list be more efficient / effective than using
other containers such as vector, deque, etc?
As far as I'm aware, list doesn't appear to be...
|
by: Spoon |
last post by:
Hello,
Could someone explain why the following code is illegal?
(I'm trying to use a list of (C-style) arrays.)
#include <list>
typedef std::list < int foo_t;
int main()
{
int v = { 12, 34...
|
by: TBass |
last post by:
So I have a class:
class Client
{
unsigned int ClientID;
....
};
class MyListenSocket
|
by: Javier |
last post by:
Hi all,
I have this code:
class A
{
std::list<Bm_observadores;
void function()
{
|
by: Ray D. |
last post by:
Hey all,
I'm trying to pass a list into a function to edit it but when I
compile using g++ I continue to get the following error:
maintainNeighbors.cpp:104: error: invalid initialization of...
|
by: isliguezze |
last post by:
template <class T>
class List {
public:
List();
List(const List&);
List(int, const T&);
void push_back(const T &);
void push_front(const T &);
void pop_back();
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |