Results 1 to 6 of 6

Thread: C++ how to initialize a vector

  1. #1
    Join Date
    Dec 2009
    Beans
    348

    C++ how to initialize a vector

    Hi,

    I have a simple question. How can I initialize the following vector?
    Code:
    std::vector<std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > > myVector;
    I would like to have 5 elements (for example) inside of myVector. i.e. the outer vector of myVector should be of size 5.
    I think that the following way is not the only one.
    Code:
    std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > tempVector(0);
    for (int i = 0; i < 5; ++i)
    {
    myVector.push_back(tempVector);
    }
    What are the other ways?

    Thank you
    Last edited by erotavlas; May 9th, 2011 at 01:31 PM.

  2. #2
    Join Date
    Nov 2009
    Location
    The Netherlands
    Beans
    239
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C++ how to initialize a vector

    You could use the constructor: std::vector(size_type n, const T& value= T(), const Allocator& = Allocator());
    It initializes the vector with its content set to a repetition, n times, of copies of value.

    Just one remark though, i think it would be better to define a class than to use std::vector<std:air<std::vector<std:air<std::string, int> >, double> > as a datatype. It would certainly make your code a lot more readable and easier to understand. If you don't want to do that, you could also define a typedef for it so you don't have to repeat the whole thing every time in your code.

  3. #3
    Join Date
    Dec 2009
    Beans
    348

    Re: C++ how to initialize a vector

    Quote Originally Posted by ziekfiguur View Post
    You could use the constructor: std::vector(size_type n, const T& value= T(), const Allocator& = Allocator());
    It initializes the vector with its content set to a repetition, n times, of copies of value.

    Just one remark though, i think it would be better to define a class than to use std::vector<std:air<std::vector<std:air<std::string, int> >, double> > as a datatype. It would certainly make your code a lot more readable and easier to understand. If you don't want to do that, you could also define a typedef for it so you don't have to repeat the whole thing every time in your code.
    Thank you for your fast answer. Can you tell me how I can do your solution?

    Can I use my code without using the tempVector?
    Code:
    std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > tempVector(0);
    for (int i = 0; i < 5; ++i)
    {
    myVector.push_back(tempVector); // myVector.push_back(0) ????
    }

  4. #4
    Join Date
    Nov 2009
    Location
    The Netherlands
    Beans
    239
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: C++ how to initialize a vector

    since that constructor uses T() as a default argument i think you should just be able to use:
    Code:
    std::vector<std::vector<std::pair<std::vector<std::pair<std::string, int> >, double> > > myVector(5);
    i haven't tested it though, maybe you would have to use
    Code:
    ..blahblah... myVector(5, tempVector);

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,220
    Distro
    Kubuntu

    Re: C++ how to initialize a vector

    Is this not easier to digest, that is, clearer to read?
    Code:
    struct Data
    {
       std::string str;
       int         val;
    }; 
    
    struct Data2
    {
       std::vector<Data> data;
       double            dbl;
    }; 
    
    std::vector<Data2> myVector;
    If you want to initialize the 'myVector' to have 5 elements:
    Code:
    std::vector<Data2> myVector(5);
    Last edited by dwhitney67; May 9th, 2011 at 02:19 PM.

  6. #6
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: C++ how to initialize a vector

    dwhitney67's solution points to a general approach you should always take in account when programming: repeated code is usually a symptom of insufficient/incorrect abstraction. This is valid in all programming languages, no matter what paradigm; of course the solutions may vary from one language to another, but the general idea is always the same: avoid repetitions by creating new concepts that allow further simplification of your code.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •