Thread: I just don't get this...

  1. #1
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717

    I just don't get this...

    Well, I'm reading "Accelerated C++ 2000" and I just don't get how a struct name can be used in the same way as int, string, double...
    Like, "vector<Student_info>::size_type i = 0;" makes no sense, Student_info doesn't let the vector accept any values like int, string or double. How can it then be used in the same way?
    Currently research OpenGL

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Of course it can accept Student_info as well as int, string or double. You should try it.
    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.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What makes you think std::string is any way similar to the built-in types like int and double and not to user-defined types like Student_info?

    Anyway, vector is a template class, so it can hold all kinds of types that meet simple requirements (copyable, assignable etc).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I've been trying it, but I don't get it :P
    The struct doesn't even have a type, what data can a vector<Student_info> store?
    if a struct was defined as type struct name, I'd maybe understand it a bit more...

    And, Student_info is also used in a simple variable defining way, like "Student_info variable_name"
    Currently research OpenGL

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So in essence, you are implying you are trying to use a type that does not exist?
    Last edited by Elysia; 11-16-2008 at 05:11 PM.
    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.

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    What are you talking about? If you mean that there's a missing "struct" keyword there then this is one difference between C++ and C. In C++ the name of the struct becomes the name of a user-defined type without any typedef.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    I'm just following the book, I wanna understand it too :P
    anyways, does "vector<Student_info> student" let me have multiple students in one vector, and each student can have his own student.name or student.grade?
    So like, student[0].name and student[1].name and so on?

    And anon, I don't know what I'm talking about, that's the problem :P
    Currently research OpenGL

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, yes, if the struct Student_info contains name or grade members.
    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.

  9. #9
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ok, then why the use of Student_info variable_name?

    oh, and in "vector<Student_info>::size_type i = 0;" why is the book using size_type? I thought I understood size_type, but this again makes no sense, to me :P
    Last edited by Akkernight; 11-16-2008 at 05:18 PM.
    Currently research OpenGL

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It creates a single Student_info.
    A vector stores several of the type. It is a dynamic array.
    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.

  11. #11
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Oh wait, that's kinda obious >.< well, thanks anyway :P
    but please explain...
    "vector<Student_info>::size_type i = 0;" why is the book using size_type? I thought I understood size_type, but this again makes no sense, to me :P
    I hope I ain't being too frustrating or something :P
    Currently research OpenGL

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    oh, and in "vector<Student_info>::size_type i = 0;" why is the book using size_type? I thought I understood size_type, but this again makes no sense, to me :P
    I guess the book is showing you the most correct (and pedantic) way to do it. This way you declare a variable that is guaranteed to be large enough to hold the size of the vector (the lazy way would probably be to use just std::size_t/unsigned/long unsigned).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    Ah... Thanks ^^
    Currently research OpenGL

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The lazy would do...
    Code:
    auto i = vec.begin();
    ...in the coming C++0x!
    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.

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Um, no. How would you do this:

    Code:
    for (std::vector<X>::size_type i = 0; i != vec.size(); ++i)
    I wonder if C++0x would allow this though
    Code:
    for (auto end = vec.size(), i = 0; i != end; ++i)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed