Random access an element in a STL List

A

Allerdyce.John

Hi,

How can I Random access an element in a STL List if I have a pointer to
that list?
I know how to do that if I have a reference to a STL list, but how can
I do that if I have a pointer to a STL List.

void function (vector<int>& myLIst) {
// access the first element of myList
printf ("%d", myList[1]);
}

but if I have this:
void function (vector<int>* myLIst) {
// how to access the first element of myList
printf ("%d", myList[1]);
}

Thank you.
 
A

Allerdyce.John

Sorry, there is a typo in my previous mail.
I meant 'second' element not 'first'
 
T

TB

(e-mail address removed) sade:
Hi,

How can I Random access an element in a STL List if I have a pointer to
that list?
I know how to do that if I have a reference to a STL list, but how can
I do that if I have a pointer to a STL List.

First, there's a huge difference between a std::list<> (list) and a
std::vector said:
void function (vector<int>& myLIst) {
// access the first element of myList
printf ("%d", myList[1]);
}

but if I have this:
void function (vector<int>* myLIst) {
// how to access the first element of myList
printf ("%d", myList[1]);

You have to dereference the pointer:

(*myList)[1];

or:

myList->at(1);

or the really, really ugly way:

myList->operator[](1);

Any c++ book should cover basic pointer use.
 
R

Roland Pibinger

(e-mail address removed) sade:

First, there's a huge difference between a std::list<> (list) and a
std::vector<> (vector).

In Java the "vector" is called ArrayList ...
 
R

Roland Pibinger

Roland Pibinger sade:

And?
Did you notice that the OP wrote "STL List" and then
used a std::vector<>?

Where does the confusion come from? Nowadays people are taught Java as
their first programming language.
 
L

Luke Meyers

What do you mean by "the vector?" Do you mean the class which most
closely resembles std::vector<> in functionality and/or implementation?
Are you aware of java.util.Vector? java.util.List? I'm not saying
it's false that, in many cases, ArrayList is an appropriate parallel
usage to std::vector, but I'd recommend getting out of the mindset that
there is a 1:1 correspondence between Java entities and C++ entities.
Where does the confusion come from?

I would say it comes from assuming that "a list is a list is a list"
rather than giving a moment's thought to the attributes of different
implementations which support various container operations. Or, better
yet, actually reading the documentation.
Nowadays people are taught Java as
their first programming language.

Yes. And nowadays people are taught C++ as their first programming
language. Also, nowadays people are taught MIT-Scheme as their first
programming language. And Python, and Ruby, and on and on. Nobody
used to learn Java first (because it didn't exist/hadn't become
sufficiently established), but now some do. There are advantages and
disadvantages to this approach, all of which is well-covered ground.
The following article provides one point of view:

http://www.joelonsoftware.com/articles/ThePerilsofJavaSchools.html

Luke
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top