Another question about multidimensional vectors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Markus Pitha

    #1

    Another question about multidimensional vectors

    Hello again,

    I have another question about the handling of multidimensiona l vectors.
    Actually, it's not difficult when the size is known at the beginning,
    but I still wasn't able to create dynamic vectors without knowing the size.
    I want to create a vector which contains a vector of ints:

    vector<vector<i nt values;

    I tried to add the value 35 to the first position of the first vector
    and this vector should be on the first position of the "main" vector.
    How can I push_back() a vector which contains ints to a vector?
    I tried it in a different way like:

    vector<intxAxis ;
    vector<xAxisyAx is;


    But I get compiling errors. I thought about using resize() but both
    vectors will be resized in a loop so I have to resize them over and over
    again. Moreover I didn't find a conclusion with resize() either. Why do
    I actually have to resize them although vectors are dynamic types?
    And how can I solve this issue?


    Markus
  • Jim Langston

    #2
    Re: Another question about multidimensiona l vectors

    "Markus Pitha" <newsgroupsNOSP AM@pithax.netwr ote in message
    news:1403f$4724 60be$54705512$2 1755@news.chell o.at...
    Hello again,
    >
    I have another question about the handling of multidimensiona l vectors.
    Actually, it's not difficult when the size is known at the beginning, but
    I still wasn't able to create dynamic vectors without knowing the size.
    I want to create a vector which contains a vector of ints:
    >
    vector<vector<i nt values;
    >
    I tried to add the value 35 to the first position of the first vector and
    this vector should be on the first position of the "main" vector.
    How can I push_back() a vector which contains ints to a vector?
    I tried it in a different way like:
    >
    vector<intxAxis ;
    vector<xAxisyAx is;
    >
    >
    But I get compiling errors. I thought about using resize() but both
    vectors will be resized in a loop so I have to resize them over and over
    again. Moreover I didn't find a conclusion with resize() either. Why do I
    actually have to resize them although vectors are dynamic types?
    And how can I solve this issue?
    This help any?

    #include <vector>

    int main()
    {
    std::vector<std ::vector<int Data;
    std::vector<int Row;
    Data.push_back( Row );

    Data[0].push_back( 11 );
    Data[0].push_back( 12 );

    if ( Data.size() < 2 )
    Data.push_back( Row );
    Data[1].push_back( 21 );
    Data[1].push_back( 22 );
    }


    Comment

    • Juha Nieminen

      #3
      Re: Another question about multidimensiona l vectors

      Markus Pitha wrote:
      How can I push_back() a vector which contains ints to a vector?
      Just resize the vector and push back the int into the first vector.
      For example:

      values.resize(1 ); // now it has 1 int-vector inside it.
      values[0].push_back(35); // Pushes 35 into the first vector.

      If you want to *literally* push a vector of ints into 'values', you
      can literally do that too. For example:

      std::vector<int aVector(1, 35); // initialized to have 1 value, 35
      values.push_bac k(aVector);
      I tried it in a different way like:
      >
      vector<intxAxis ;
      vector<xAxisyAx is;
      I don't really understand what is it that you tried to accomplish
      with that. If what you want is to create an x*y double-vector, you can
      do it like this:

      std::vector<std ::vector<int values(xSize, std::vector<int >(ySize));

      Now 'values' will be a vector with xSize vectors inside it, and each
      one of these will have ySize ints inside them. Then you can simply
      access the values like values[2][3].
      Why do
      I actually have to resize them although vectors are dynamic types?
      You have to resize them *because* they are dynamic types, not *although*.

      Comment

      • Markus Pitha

        #4
        Re: Another question about multidimensiona l vectors

        Hello,

        Jim Langston wrote:
        #include <vector>
        >
        int main()
        {
        std::vector<std ::vector<int Data;
        std::vector<int Row;
        Data.push_back( Row );
        >
        Data[0].push_back( 11 );
        Data[0].push_back( 12 );
        >
        if ( Data.size() < 2 )
        Data.push_back( Row );
        Data[1].push_back( 21 );
        Data[1].push_back( 22 );
        }
        Thanks, that helped me to solve my problem.

        Markus

        Comment

        • Markus Pitha

          #5
          Re: Another question about multidimensiona l vectors

          Hello,

          Juha Nieminen wrote:
          std::vector<int aVector(1, 35); // initialized to have 1 value, 35
          values.push_bac k(aVector);
          std::vector<std ::vector<int values(xSize, std::vector<int >(ySize));
          But I didn't know the size. The size will be increased in a recursion,
          so I had problems with the right syntax. Jim's example already lead my
          to my target.

          Markus

          Comment

          • Jim Langston

            #6
            Re: Another question about multidimensiona l vectors

            "Markus Pitha" <newsgroupsNOSP AM@pithax.netwr ote in message
            news:286ef$4724 7b77$54705512$9 710@news.chello .at...
            Hello,
            >
            Jim Langston wrote:
            >
            >#include <vector>
            >>
            >int main()
            >{
            > std::vector<std ::vector<int Data;
            > std::vector<int Row;
            > Data.push_back( Row );
            >>
            > Data[0].push_back( 11 );
            > Data[0].push_back( 12 );
            >>
            > if ( Data.size() < 2 )
            > Data.push_back( Row );
            > Data[1].push_back( 21 );
            > Data[1].push_back( 22 );
            >}
            >
            Thanks, that helped me to solve my problem.
            Incidently, if you don't want to have to create a variable to push back you
            could just do:

            #include <vector>

            int main()
            {
            std::vector<std ::vector<int Data;
            std::vector<int Row;
            Data.push_back( std::vector<int >() );

            Data[0].push_back( 11 );
            Data[0].push_back( 12 );

            if ( Data.size() < 2 )
            Data.push_back( std::vector<int >() );
            Data[1].push_back( 21 );
            Data[1].push_back( 22 );
            }


            Comment

            • Tadeusz Kopec

              #7
              Re: Another question about multidimensiona l vectors

              Markus Pitha wrote:
              [snip]
              I tried it in a different way like:
              >
              vector<intxAxis ;
              Here you are declaring a variable xAxis.
              vector<xAxisyAx is;
              Here you treat xAxis as a type name.

              It should be
              typedef vector<intxAxis ;
              vector<xAxisyAx is;
              to compile, if I correctly guess what you mean.

              Comment

              • BobR

                #8
                Re: Another question about multidimensiona l vectors


                Juha Nieminen wrote in message...
                Markus Pitha wrote:
                How can I push_back() a vector which contains ints to a vector?
                >
                Just resize the vector and push back the int into the first vector.
                For example:
                >
                values.resize(1 ); // now it has 1 int-vector inside it.
                values[0].push_back(35); // Pushes 35 into the first vector.
                >
                If you want to *literally* push a vector of ints into 'values', you
                can literally do that too. For example:
                >
                std::vector<int aVector(1, 35); // initialized to have 1 value, 35
                values.push_bac k(aVector);
                >
                I tried it in a different way like:

                vector<intxAxis ;
                vector<xAxisyAx is;
                >
                I don't really understand what is it that you tried to accomplish
                with that. If what you want is to create an x*y double-vector, you can
                do it like this:
                >
                std::vector<std ::vector<int values(xSize, std::vector<int >(ySize));
                For that, all you need is:

                size_t xSize( 20 ), ySize( 30 );
                std::vector<std ::vector<int values( xSize, ySize ); // 20x30

                --
                Bob R
                POVrookie


                Comment

                Working...