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?

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.

Edited 7 Years Ago by Rashakil Fol: n/a

Comments
Yes!

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

And considering Stroustrup invented C++ I'd consider it a good book

What? Are you mentally retarded?

s/good/accurate/

Edited 7 Years Ago by ShawnCplus: n/a

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)

Rashakil why the hell did you insert the "retarded-mentally" tag in my thread?

Edited 7 Years Ago by neithan: n/a

Huh? I didn't insert any tags.

Uhm that's weird then. Check out the tags there's a "retarded-mentally" tag lol?

Greets.

This article has been dead for over six months. Start a new discussion instead.