473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Random access an element in a STL List

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.

Feb 4 '06 #1
6 6866
Sorry, there is a typo in my previous mail.
I meant 'second' element not 'first'

Feb 4 '06 #2
TB
Al************@gmail.com 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<> (vector).
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.

--
TB @ SWEDEN
Feb 4 '06 #3
On Sat, 04 Feb 2006 20:48:53 +0100, TB <TB@SWEDEN> wrote:
Al************@gmail.com sade:
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<> (vector).


In Java the "vector" is called ArrayList ...
Feb 4 '06 #4
TB
Roland Pibinger sade:
On Sat, 04 Feb 2006 20:48:53 +0100, TB <TB@SWEDEN> wrote:
Al************@gmail.com sade:
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<> (vector).


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


And?

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

--
TB @ SWEDEN
Feb 4 '06 #5
On Sat, 04 Feb 2006 21:26:33 +0100, TB <TB@SWEDEN> wrote:
Roland Pibinger sade:
In Java the "vector" is called ArrayList ...


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.

Feb 5 '06 #6
Roland Pibinger wrote:
On Sat, 04 Feb 2006 21:26:33 +0100, TB <TB@SWEDEN> wrote:
Roland Pibinger sade:
In Java the "vector" is called ArrayList ...

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.
And?
Did you notice that the OP wrote "STL List" and then
used a std::vector<>?
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/articl...vaSchools.html

Luke

Feb 5 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brandon Michael Moore | last post by:
I'm trying to test a web application using a tool written in python. I would like to be able to generate random values to put in fields. I would like to be able to generate random dates (in a...
4
by: Bart Nessux | last post by:
New to Python... trying to figure out how to count the objects in a list and then map the count to the objects or convert the list to a dict... I think the latter would be better as I need a number...
2
by: vsgdp | last post by:
From what I learned, if you want to do random element insertions and deletions you should use a list. But, with std::vector, if the order of the elements does not matter, couldn't you efficiently...
9
by: gl | last post by:
How do I take an array or arraylist, and sort it randomly? Like suppose the items in it are (1,2,3,4,5) and I want to get it to be in a random order (2,3,1,4,5). How do you do that? I think it's a...
5
by: flamesrock | last post by:
Hi, It's been a while since I've played with python. My question is... whats the best way to pop a random item from a list?? -Thanks
19
by: Boris Borcic | last post by:
does x.sort(cmp = lambda x,y : cmp(random.random(),0.5)) pick a random shuffle of x with uniform distribution ? Intuitively, assuming list.sort() does a minimal number of comparisons to ...
2
by: srpiton | last post by:
Hello everybody. Anybody has any ideas with the following problem? We start with a list, and we wish to construct a generator that yields each element of the list exactly once, but in random...
13
by: Bruza | last post by:
I need to implement a "random selection" algorithm which takes a list of as input. Each of the (obj, prob) represents how likely an object, "obj", should be selected based on its probability of...
15
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.