LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-02-2005, 08:17 AM   #1
markhod
Member
 
Registered: Sep 2003
Posts: 103

Rep: Reputation: 15
how to access elements of std::vector<int>*


Hello,

What I want is to make a pointer to an std::vector, to put something in it and then retrieve the value. All I can find that will compile is:

std::vector<Int_t>* test = new std::vector<Int_t>;
test->push_back(5);
Int_t test5 = test[5000][0];

I dont understand why though or what the first [5000] means (5000 can be any number to make it compile). I cant find any help from google or previous posts. Does anyone know?

I am using gcc 3.2.3 on linux.

Thanks,

Mark
 
Old 08-02-2005, 09:00 AM   #2
biertrinken
LQ Newbie
 
Registered: Dec 2004
Location: bayern
Posts: 23

Rep: Reputation: 15
you have to dereference the test pointer
before using the index-operator

Int_t test5 = (*test)[0];

a std::vector is not the same as a 2 dimensional array !

tobi
 
Old 08-02-2005, 09:03 AM   #3
markhod
Member
 
Registered: Sep 2003
Posts: 103

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by biertrinken
you have to dereference the test pointer
before using the index-operator

Int_t test5 = (*test)[0];

a std::vector is not the same as a 2 dimensional array !

tobi
oh, I see. I had been trying *test[0] and it wouldnt work. I guess brackets will help then!

I still dont see why test[5000][0][ compiles given it isnt a 2D thing though.

Thanks,

Mark
 
Old 08-02-2005, 10:17 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
It compiles, but l'm guessing you will segfault when the first element is anything other than 0. Pointers can be dereferenced as though they were an array as well. For instance:

Code:
int *p1 = new int[10];
int *p2 = new int;

*p1 = 10; // OK, same as p1[0] = 10;
p2[0] = 10;  // OK, same as *p2 = 10;

p1[9] = 10; // Ok, since there are 10 elements
p2[9] = 10; // NOT ok, it only points to 1 int not an array
That last line is a segfault waiting to happen...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to sort std::vector<double>? markhod Programming 4 10-12-2005 04:21 AM
std::vector <> ::max_size ta0kira Programming 1 04-29-2005 04:59 AM
when modify the int 80 interrupt vector i run into some questions qqrilxk Programming 1 04-18-2005 01:13 PM
can i modify int 80 vector to a user-defined vector qqrilxk Linux - Security 1 03-03-2005 08:46 PM
C++ std::string to int Slaxx Programming 1 10-30-2004 10:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration