C++ beginner, vector<> or array[] ?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training

C++ beginner, vector<> or array[] ?

 
0
  #1
Oct 8th, 2009
I'm new to C++, and i've seen some C and C++ code.
I tried array[] and vector<> so i see i can use both, why should i use what way to work with vectors?
Furthermore, i've seen a lot of "array[]" in this forum... so, any lead?

Thank you!

Oh by the way, i'm learning with the "Principles and Practice Using C++" by Stroustrup, what do you think? I'm in the good direction? Any recommendations?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
5
  #2
Oct 8th, 2009
Originally Posted by neithan View Post
I'm new to C++, and i've seen some C and C++ code.
I tried array[] and vector<> so i see i can use both, why should i use what way to work with vectors?
Furthermore, i've seen a lot of "array[]" in this forum... so, any lead?
Just use vector<>. Never use arrays. I mean, sure, at some point in time, you'll want to learn how to use them. They are useful for implementing vectors, after all. And sometimes, occasionally, rarely, a fixed-size array is just what you want and you can't accept the miniscule overhead that using a vector gives you. People use arrays in this forum usually because they are taking classes with out-of-date textbooks or out-of-date teachers.

The main problem with arrays is that you generally have to fill the array with values, when you want to use it. And this means you've first got to create the array, and you have to know what size it is, and then you have to put the values into it. Lots of times newbies on this forum will create an array _larger_ than the amount of stuff they're putting in it, and then, when it has too much stuff, they just throw an error. Or worse, they keep trying to put more stuff in, writing into memory past the end of the array, creating undefined behavior that's hard to debug. And then you've got to keep track of how much stuff you've put in the array.

With std::vector, you just create the vector, and then append stuff using push_back. So just worry about std::vector<> and std::string for now. Occasionally you'll need to use a char*, like when you need to provide a filename for opening a file. Plenty of APIs will expect arrays, though, and that's just life.

I don't know whether the book is good, but I know the author is good.
Last edited by Rashakil Fol; Oct 8th, 2009 at 6:04 pm.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,429
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 231
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey
 
0
  #3
Oct 8th, 2009
Originally Posted by neithan View Post
I'm new to C++, and i've seen some C and C++ code.
I tried array[] and vector<> so i see i can use both, why should i use what way to work with vectors?
Furthermore, i've seen a lot of "array[]" in this forum... so, any lead?

Thank you!

Oh by the way, i'm learning with the "Principles and Practice Using C++" by Stroustrup, what do you think? I'm in the good direction? Any recommendations?
Well vectors are an expandable data structure whereas arrays are static. Once you define the size of an array it pretty much stays there (yeah, there are ways to increase them but not easily). Vectors were made to be expandable and best used when you have an undetermined number of items you need.

And considering Stroustrup invented C++ I'd consider it a good book though C++ The Programming Language is another good one from Bjarne
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
2
  #4
Oct 8th, 2009
Originally Posted by ShawnCplus View Post
And considering Stroustrup invented C++ I'd consider it a good book
What? Are you mentally retarded?
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 1,429
Reputation: ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light ShawnCplus is a glorious beacon of light 
Solved Threads: 231
Sponsor
ShawnCplus's Avatar
ShawnCplus ShawnCplus is offline Offline
Code Monkey
 
0
  #5
Oct 8th, 2009
Originally Posted by Rashakil Fol View Post
And considering Stroustrup invented C++ I'd consider it a good book
What? Are you mentally retarded?
s/good/accurate/
Last edited by ShawnCplus; Oct 8th, 2009 at 6:11 pm.
GCS d- s+ a-->? C++(++++) UL+++ P+>+++ L+++ E--- W+++
N+ o K w++(---) O? !M- V PS+>++ PE+ Y+ PGP !t- 5? X- R tv+
b+>++ DI+ D G++>+++ e+ h+>++ r y+
PMs asking for help will not be answered, post on the forums. That's what they're there for.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
3
  #6
Oct 8th, 2009
Originally Posted by ShawnCplus View Post
s/good/accurate/
I was asking a yes or no question, and a simple "no" would have sufficed
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #7
Oct 8th, 2009
Awesome explanation, i think i got the idea. So i'll stick with vector and string although i'll have to it array anyways since as you say it's used in most libraries.

Thank you very much guys!

PD: i asked if i was in the good direction with this book since it's 1200 pages long! LOL (i'm at pg 140 already haha, just a few to go)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #8
Oct 8th, 2009
Rashakil why the hell did you insert the "retarded-mentally" tag in my thread?
Last edited by neithan; Oct 8th, 2009 at 6:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,055
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter
 
3
  #9
Oct 8th, 2009
Originally Posted by neithan View Post
Rashakil why the hell did you insert the "retarded-mentally" tag in my thread?
Huh? I didn't insert any tags.
All my posts may be redistributed under the GNU Free Documentation License.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
1
  #10
Oct 9th, 2009
Originally Posted by Rashakil Fol View Post
Huh? I didn't insert any tags.
Uhm that's weird then. Check out the tags there's a "retarded-mentally" tag lol?

Greets.
Reply With Quote Quick reply to this message  
Reply

Tags
beginner, retarded-mentally

Message:




Views: 834 | Replies: 9
Thread Tools Search this Thread



Tag cloud for beginner, retarded-mentally
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC