locked
Vectors in C++11 RRS feed

  • Question

  • I have Bjarne Stroustrup's Principals and Practice with the C++ Programming Language.  In chapter 4, he talks about Vectors and has me compile some code that is similar to this: vector<int> name[4].  I was teaching myself with an online tutorial, and the author never said anything about Vectors; he just had me use name[4] and said that the compiler would figure it out.  That works in my Visual Studio Express 2012, and the vector<int> name[4] version doesn't.  Both authors are using Visual Studio 2005.  How does this work out?  The 'vector' keyword is not even a keyword in my compiler.  Was this changed since Visual Studio 2005?  What is a good book to learn C++11 since Mr. Stroustrup's didn't work?

    Abraham Hosch

    Saturday, March 9, 2013 1:49 AM

Answers

  • >That works in my Visual Studio Express 2012, and the
    >vector<int> name[4] version doesn't.

    In what way *exactly* doesn't it "work"?
    Is there an error message you'd like to share?

    For starters, to use vector<> in a program you need:

    #include <vector>

    Secondly, vectors are in the std namespace. So you need to
    specify that. e.g. - One of these:

    std::vector<int> ...

    or

    using namespace std; // global

    vector<int> ...

    or

    using std::vector; // specific

    vector<int> ...

    - Wayne
    • Marked as answer by Hosch027 Saturday, March 9, 2013 4:02 PM
    Saturday, March 9, 2013 2:25 AM
  • I have Bjarne Stroustrup's Principals and Practice with the C++ Programming Language. In chapter 4, he talks about Vectors and has me compile some code that is similar to this: vector<int> name[4].
    In addition to Wayne's answer, the statement
     
    vector<int> name[4];
     
    declares an array of 4 empty vectors of type int, not a vector of type int and length 4.
     
    You probably meant:
     
    vector<int> name(4);
     
    I am not familiar with the Stroustrup book you mention, so I do not know how easy it is for a beginner, but it is hard for me to believe that the inventor of the C++ language failed to tell you the things contained in Wayne's response.
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Hosch027 Saturday, March 9, 2013 4:02 PM
    Saturday, March 9, 2013 9:03 AM
  • vector<int> name[4];
     
    declares an array of 4 empty vectors of type int,

    It would seem a little out of place for Bjarne to be using a
    "raw" array of vectors - unless to illustrate some potential
    problems with using them. In the code fragments collection
    for that book from his web site I don't see any such use
    of an array of vectors in Chapter 4. Neither do I see any
    mention of same in the Errata. So the OP may have misread
    the example. However, it is valid code and should compile
    without errors. There also should be no errors when used
    properly. So whether it "works" depends on what the OP is
    doing and expecting - neither of which is apparent.

    - Wayne

    • Marked as answer by Hosch027 Saturday, March 9, 2013 4:02 PM
    Saturday, March 9, 2013 9:42 AM

All replies

  • >That works in my Visual Studio Express 2012, and the
    >vector<int> name[4] version doesn't.

    In what way *exactly* doesn't it "work"?
    Is there an error message you'd like to share?

    For starters, to use vector<> in a program you need:

    #include <vector>

    Secondly, vectors are in the std namespace. So you need to
    specify that. e.g. - One of these:

    std::vector<int> ...

    or

    using namespace std; // global

    vector<int> ...

    or

    using std::vector; // specific

    vector<int> ...

    - Wayne
    • Marked as answer by Hosch027 Saturday, March 9, 2013 4:02 PM
    Saturday, March 9, 2013 2:25 AM
  • I have Bjarne Stroustrup's Principals and Practice with the C++ Programming Language. In chapter 4, he talks about Vectors and has me compile some code that is similar to this: vector<int> name[4].
    In addition to Wayne's answer, the statement
     
    vector<int> name[4];
     
    declares an array of 4 empty vectors of type int, not a vector of type int and length 4.
     
    You probably meant:
     
    vector<int> name(4);
     
    I am not familiar with the Stroustrup book you mention, so I do not know how easy it is for a beginner, but it is hard for me to believe that the inventor of the C++ language failed to tell you the things contained in Wayne's response.
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by Hosch027 Saturday, March 9, 2013 4:02 PM
    Saturday, March 9, 2013 9:03 AM
  • vector<int> name[4];
     
    declares an array of 4 empty vectors of type int,

    It would seem a little out of place for Bjarne to be using a
    "raw" array of vectors - unless to illustrate some potential
    problems with using them. In the code fragments collection
    for that book from his web site I don't see any such use
    of an array of vectors in Chapter 4. Neither do I see any
    mention of same in the Errata. So the OP may have misread
    the example. However, it is valid code and should compile
    without errors. There also should be no errors when used
    properly. So whether it "works" depends on what the OP is
    doing and expecting - neither of which is apparent.

    - Wayne

    • Marked as answer by Hosch027 Saturday, March 9, 2013 4:02 PM
    Saturday, March 9, 2013 9:42 AM
  • Thank you for all your helpful replies.  He wants me to download and install a pre-written header file that contains #include <vector>, but I would rather just keep putting in my own headers.  The trouble was, I had just forgot to include the #include <vector>.  You are correct that I should have written vector<int> name(4).  Thanks again for your help.

    Abraham Hosch

    Saturday, March 9, 2013 4:08 PM