How to use at for STL list
-
Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n
-
Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n
If you need direct access to an element at a specific index in your list, why don't use a vector instead of a list ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n
std::list is not a "random access" sequence (i.e. cannot use a random access iterator).
Watched code never compiles.
-
Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n
-
As Richard and the others have said, use vector if you can. If you really really really must have at-like functionality you can use std::advance to do it. Cheers, Ash
-
Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n
What about:
template <typename T> class mylist: public list<T>
{
public:
T & at(int n)
{
int i;
list::iterator it= this->begin();
i = 0;
while (it != this->end())
{
if ( i==n ) return *it;
i++; it++;
}
throw( out_of_range("out of mylist range"));
}
};If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What about:
template <typename T> class mylist: public list<T>
{
public:
T & at(int n)
{
int i;
list::iterator it= this->begin();
i = 0;
while (it != this->end())
{
if ( i==n ) return *it;
i++; it++;
}
throw( out_of_range("out of mylist range"));
}
};If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
What about:
template <typename T> class mylist: public list<T>
{
public:
T & at(int n)
{
int i;
list::iterator it= this->begin();
i = 0;
while (it != this->end())
{
if ( i==n ) return *it;
i++; it++;
}
throw( out_of_range("out of mylist range"));
}
};If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]depending on how the at() is used you can shortcut operations by holding the current iterator around as long as add/delete/insert operations have not been done. Using the difference between last at() and current at() you can move up or down the list. It takes some work, but if you are doing big searches you wouldn't always want to start at the beginning unless the new at() command is closer to the start or the end. :) and if you do add/delete/insert, just invalidate the iterator, and start over as you have done.
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
-
Hi Experts I need at() feature in my STL List. can anyone help me out? With Regards R e h a n
If you use at() often you might consider something like a skip-list: http://www.codersource.net/microsoft-net/c-advanced/skip-list-a-simpler-alternative-to-binary-trees.aspx[^] Skip lists would allow you to skip sequentially starting from the beginning on ever at() command.
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
-
depending on how the at() is used you can shortcut operations by holding the current iterator around as long as add/delete/insert operations have not been done. Using the difference between last at() and current at() you can move up or down the list. It takes some work, but if you are doing big searches you wouldn't always want to start at the beginning unless the new at() command is closer to the start or the end. :) and if you do add/delete/insert, just invalidate the iterator, and start over as you have done.
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
Clever. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Clever. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]CPallini wrote:
Clever.
naw... comes from using skiplists, which made me think that wouldn't be a bad method here if there aren't many add-deletes, skip-lists let you jump around lists rapidly.
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
-
CPallini wrote:
Clever.
naw... comes from using skiplists, which made me think that wouldn't be a bad method here if there aren't many add-deletes, skip-lists let you jump around lists rapidly.
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
Well, skiplists (I didn't know about) are clever, at lest... :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]