1

in c++

like I have

        vector<vector<string> > input;
         for(int i=0;i<=1000;i++){

          vector<string> temp;
          temp.pushback(everytime change the input value);
                      .
                      .
                      .just continues push some string in temp
                      .
                      .
           temp.pushback(everytime change the input value);
          input.pushback(temp);
         }

my question is that, if the we put some string in temp for about 1000 times, will these temp share the address? like when i=500, the new temp will use the address of my first temp address which created when i=1? or although the vector(string) use the same name like temp, the address will always be different.

cause I want to make something like a 2D dynamic Array, so I think about

           vector<vector<string> > input;

, and what I need is every-string-type vector temp should be safely keep input. is there any butter idea other than

              vector<vector<string> > input;
Rebecca
  • 111
  • 2
  • 12

4 Answers4

4

Your code is leaking; when you write

vector<string> temp = *new vector<string>;

you are allocating an empty vector<string> on the heap, then copying it on temp. Each time you make this loop you leak a vector (an empty one, so probably just a few bytes).

This allocation is not needed at all, you could just write instead:

vector<string> temp;

Moreover you are creating a temporary vector, filling it and and the pushing it in another vector. A better approach would probably be instead:

  input.resize(1 + input.size());       // make room for a new vector
  vector<string>& temp = input.back();  // call it temp in the following
  ...
  temp.push_back(...);

this way you are allocating the new vector already inside input and adding elements to it directly... temp works just as a temporary "nickname" for the newly added vector.

6502
  • 112,025
  • 15
  • 165
  • 265
  • thanks for answering, cause I want to create something like a 2D dynamic array, and the amount of temp is vary, and I don't know how many it will be, it can be 500 or 1000 or more, that mean actually i – Rebecca Apr 23 '13 at 10:29
0

In this case you will have copy of your string each time you push back it in your vector. If you don't want to copy your variable - you can use pointers, for example (which is not common practice for std::string, but you can use some std::ref magic, but again, this is another discussion)

Wintermute
  • 1,501
  • 17
  • 22
0

It won't work that way. The solution might be much easier, just use:

vector<string> temp; // no new ...

Since you probably want to store the copies of that vector in the overall data holder input (of type vector<vector<string>>.

It might not be the most efficient solution, but it should work reliably. Each instance will be independent of the other instances.

You should, however, move the lines around a but to make sure you only have one overall input:

 vector< vector< string > > input; // move this line *before* the loop!
 for(int i=0;i<=1000;i++){
      vector<string> temp;
      temp.pushback(everytime change the input value);
                  .
                  .
                  .just continues push some string in temp
                  .
                  .
      temp.pushback(everytime change the input value);
      input.pushback(temp);
 }

otherwise you will create a new input with each loop and throw away the previous entries. Maybe that was causing the confusion as your input only contained one valid element.

Daniel Frey
  • 55,810
  • 13
  • 122
  • 180
  • thanks for answering!! so what's the most efficient way? cause the temp.size might vary depend on different situation, and I need to track the data from input vector in the following code; – Rebecca Apr 23 '13 at 10:19
  • With C++11, it's probably `input.pushback(std::move(temp));`, with C++03: `input.pushback(vector()); input.back().swap(temp);` – Daniel Frey Apr 23 '13 at 10:26
0

As you are creating a new vector temp= *new vector; & then you try to push-back in this vector. & for improvement read the answer of @6502.

And as you are thinking that if you put some string multiple times then will it share the same address.

The answer will be "NO". Because as the vector size increases or new member is about to pushed back & the size of vector is not able to add new data then it resizes it internally & this process is done as 1-> making the room for new input(the new size depends on the internal algorithm which decides what will be the new size you can assume as double of the current size). then the whole vector entries are coppied in new area & then the new data is pushed-back.

So The allocation of new area will cause the change of the temp address.

for more info refer How does c++ std::vector work?

Community
  • 1
  • 1
akp
  • 1,753
  • 3
  • 18
  • 26
  • thanks for your reply! what about char a='t'; will the a have the duplicate address if it circulate 100 or more times? – Rebecca Apr 23 '13 at 10:36