Thread: Some questions with strings, vectors, references...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    182

    Some questions with strings, vectors, references...

    I am new to C++. My biggest problem is that I already know C. I thought learning C++ would be super easy since I've got my 1 year experience in C, but it is not so.

    Strings:
    OK, what I understand about strings is that the std::string type is a class. What I understand about classes is that they are like C structs, but they have additional features like member functions which act on an instance of a class, overloaded operators and stuff I've never heard before.

    1. How long can a string be? Seeing some examples it looks like a string can get as long as you have memory to hold them. Am I right?
    2. Can I concatenate stuff to a string, making it grow larger and larger?
    3. In C I use strtoull (all the strto familly) or atof or atoi for converting strings to numbers. What can I use in C++?

    Vectors:
    They seem like arrays to me. But can they also grow larger and larger until you have no more memory? Why would I use arrays then, since it seems that vectors have killed them?

    References:
    I would normally pass an address to a function that takes a pointer. Are references like passing the address of the variable? What's the difference between passing by reference and passing the variables address to a pointer?

    That's all for now, thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Strings:
    1. Yes, you're right. (Or at least close enough).
    2. Yes. You can use += to add to a string, or append, or a bunch of other functions.
    3. Use stringstreams in <sstream>, or boost's lexical_cast.

    Vectors:
    I've yet to hear a good reason why anyone would use a dynamic array over a vector. People still use dynamic arrays because books and teachers are slow to catch up. Local, statically sized C style arrays are often easier to use than vectors for simple tasks, though. It's just the dynamically-sized arrays that have been "killed".

    References:
    Yes, it is like that, although not the same. Differences between a function taking a reference argument and a pointer argument include the different syntax of a pointer, the fact that the pointer can be null, and the fact that the pointer can be re-assigned.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. Yes.
    2. Yes.
    3. Look up string streams
    4. Yes and backward compatibility.

    Use vectors as much as you can unless absolutely necessary that you use an array (say calling a C function). Even then, C++ makes it easy to keep such knowledge to a small part of the program.

    A reference can never be NULL.
    A reference can't be reassigned.
    Both of which make it safer than a pointer.

    <beaten all over>
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For 3)
    float f = boost::lexical_cast<float>(mystring);
    int n = boost::lexical_cast<int>(mstring);

    Boost makes life easy!
    Of course, it's not limited to these types either.
    And you can extend it to work with your own types once you get to know C++ a little better!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Thanks a lot. C++ rocks.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sure does
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by samus250 View Post
    Thanks a lot. C++ rocks.
    You'll love it more and learn faster with a good, free, electronic book.

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    What I understand about classes is that they are like C structs
    They are structs, with the difference that their attributes (variables, member functions etc) are private by default, whereas they are public by default for structs. Note that the keywords "private" and "public" can also be used for structs. Structs can also have member functions, can be templatised etc.

    Can I concatenate stuff to a string
    yes you can: insert, append (both members), reverse, rotate, partition, merge, etc etc etc (STL Algorithm).

    Vectors: They seem like arrays to me.
    They are, but supplied with iterators and dynamic memory allocation (resizing). Iterators are what make the STL library powerful.
    You could also use valarray (said to be faster), but I've read that it is not recommended by the author itself:
    http://nkari.uw.hu/Tutorials/CPPTips/musings_valarray

    There are differences in passing a pointer or a reference: a pointer should be used when the argument is optional (since pointers may be NULL and references must be initialized). Pass by 'const' reference or pointer if the "pointed-to-object" must not be changed: this is called const correctness. It'll allow the compiler to help you spot unwanted modifications.
    Last edited by CornedBee; 07-10-2008 at 02:11 PM.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> They are structs
    I think the OP was referring to C structs, and classes in C++ are quite different than structs in C.

    >> A nice site is this one
    That site is actually not legal, but the book it displays, C++ Coding Standards, is an excellent one for C++ programmers (and is probably my favorite C++ book). I'd certainly recommend paying for it if you can.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    182
    Daved: Yeah, that's what I was referring too.

    About books:
    I have Accelerated C++, but it is pretty accelerated hehe. And since it doesn't actually contrast between C and C++ then I had those questions. I also bought Stroustroup's "The C++ Programming Language", but it still hasn't arrived. I bought it because I have K&R "The C Programming Language" and it has helped me a lot since the author obviously know every single detail about the language (at least core language), and it's very great for references.
    Last edited by samus250; 07-10-2008 at 02:16 PM.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You've made good book choices, IMO. Once you've worked your way through Accelerated C++, if you feel comfortable with what you've learned then I'd recommend C++ Coding Standards as your third C++ book. If you're not comfortable, then I'd get the latest edition of C++ Primer (not C++ Primer Plus) first before C++ Coding Standards.

  12. #12
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Although the book of Stroustrup is for a reference to me, not a learning book. As medievalelks suggested, the book "Thinking in C++" is maybe one of the best books to learn from. Together with Accelerated C++ you'll come very far!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM