473,770 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6892
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.Vecto r? 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
3702
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 specified range), random strings (specifying allowed characters and a distribution of lengths), or choose randomly between several generators (for better control of the distribution of values). Is there any library for this sort of thing in Python?...
4
2963
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 associated with each entry. Any pointers? Also, does this bit of code look to be truely random? def random_number_gen(): winner = winner.append(random.sample(xrange(100000), 1))
2
4742
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 remove a random element by swapping it with the last element and then just using pop_back? Does erase do this internally? Or does erase do the element shift to fill in the gap?
9
28839
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 call to the array or array lists sort method, but i'm not exactly sure how you do it.
5
16838
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
3087
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 achieve the sort, I'd say the answer is yes. But I don't feel quite confortable with the intuition... can anyone think of a more solid argumentation ?
2
1771
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 order. In other words, an iterator version of random.shuffle(list) The motivation is the following: suppose you have a huge list and wish to extract one random element that satisfies a certain property. Typically, you expect many elements to...
13
2368
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 "prob".To simplify the problem, assuming "prob" are integers, and the sum of all "prob" equals 100. For example, items = The algorithm will take a number "N", and a list as
15
2732
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 pick from a list a random element that satisfies a given property. All or none of the elements may have the property. Most of the time, many of the elements will satisfy the property, and the property is a bit expensive to evaluate. Chance of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.