stl::list subscript operator? 
Author Message
 stl::list subscript operator?

The program below creates an STL list, then attempts to cycle through that
list using a subscript, rather than an iterator. Any ideas on how to access
STL list elements using a subscript???

Thanks,
Randy Bone

/****************************************************/
#include <iostream>
#include <list>

using namespace std;

void main(void)
{
 cout << "Hello\n";

 typedef list<int> myListType;

 myListType myList;
 myListType::iterator myListIter;

//Load the list
 for(int i=0; i<10; i++)
  myList.push_back(i);

//Try to access list elements using subscript, rather than iterator
 for(i=0; i<10; i++)
  cout << myList[i] << endl;    //this line generates a compile error,
saying [ operator not defined....

Quote:
}



Wed, 19 May 2004 03:30:03 GMT  
 stl::list subscript operator?

Quote:
> The program below creates an STL list, then attempts to cycle through that
> list using a subscript, rather than an iterator. Any ideas on how to
access
> STL list elements using a subscript???

You can't.  The standard provides no gurantee that the elements contained in
a list be contiguous and std::list does not have an index operator.  For
that, there's vector (and valarray).

Sean



Wed, 19 May 2004 03:47:11 GMT  
 stl::list subscript operator?
Thanks. I'll just cycle through to the element I want using the iterator. A
bit clunky, but sometimes that's the way it goes...

Randy


Quote:


> > The program below creates an STL list, then attempts to cycle through
that
> > list using a subscript, rather than an iterator. Any ideas on how to
> access
> > STL list elements using a subscript???

> You can't.  The standard provides no gurantee that the elements contained
in
> a list be contiguous and std::list does not have an index operator.  For
> that, there's vector (and valarray).

> Sean



Wed, 19 May 2004 05:08:31 GMT  
 stl::list subscript operator?

Quote:
> Thanks. I'll just cycle through to the element I want using the iterator.
A
> bit clunky, but sometimes that's the way it goes...

it = myList.begin();
std::advance(it,amt);

It's more clunky than indexing, but it also makes sure you know you're doing
a (relatively) costly operation.  People expect indexing to be constant or
near constant time (lgN time in the case of a mapping).

Ken



Wed, 19 May 2004 06:09:35 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. [] Operator (Outside of the subscript context)

2. MFC CString operator= conflicts with stl operator template

3. how to use STL list ( how to get a element from list)

4. STL app spends 25% of time in operator delete(void*)

5. problem with global operator new/delete and STL string

6. Problem with STL vector and assignment operator

7. STL operator problems

8. Global overload of operator delete and STL containers

9. = operator implementation in STL derived class

10. Help! Double-Linked List, Templates, and Operator Overloading

11. Error C2143 defining a typedef for a stl list

12. STL list container question

 

 
Powered by phpBB® Forum Software