-1

The push_back method of the std::vector isn't putting the second console input in the v[1] slot, it keeps overwriting it in the v[0]

I tried to search for other answers to this but the code and answer is too complicated for me to follow, im trying to keep it simple (But i tried using pointers, just got a bunch of errors)

My Method:

      vector<string> createtable(std::vector<std::string> v, std::string insertedstr) {
        std::vector<std::string> vcreate;
        vcreate.push_back(insertedstr + " ");

            return vcreate;
}

Main:

                   int main()
                {
                    int option;
                    std::string insertedstr;
                    std::vector<std::string> v;
                cin >> insertedstr;
                   v = createtable(v, insertedstr);

                for (int i = 0; i <=v.size(); i++) {
                    cout << v[i] << endl;
        }
        cin >> insertedstr;
   v = createtable(v, insertedstr);
            for (int i = 0; i <= v.size(); i++) {


    cout << v[i] << endl;
    }
        return 0;

        }

Edit: I want to eventually write a menu for this so I want to have an infinite amount of push_backs, so just calling v.push_back in the main won't work for me

Would be great if someone could help.

donnie
  • 17
  • 1
  • 7

3 Answers3

2

You're creating a new vector in each call to createTable, not reusing an existing vector; you're not constantly inserting into v[0], you're constantly replacing v with a whole new vector that only has a single element. The second call to createTable should probably just be a direct call to v.push_back(insertedString);.

Alternatively, remove the vcreate declaration and actually use the v passed into the function instead (which is still wasteful, because it's constantly copying and replacing vectors instead of pushing onto an existing one directly, but it would at least be logically correct).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Well I want to eventually write a menu for this that can add infinitely number of elements to the vector – donnie Sep 30 '15 at 04:27
  • Then remove the declaration/use of `vcreate`, stop returning anything from `createTable`, and have `createTable` accept the first argument by reference `void createtable(std::vector& v, std::string insertedstr) { insertedstr += " "; v.push_back(insertedstr); }`. Will save an awful lot of expensive copies recreating your "infinite" element `vector`. – ShadowRanger Sep 30 '15 at 04:31
  • Well but then it won't store anything at all into the vector v – donnie Sep 30 '15 at 04:41
  • You'd stop reassigning `v` outside the function, because the function call would be passing `v` in by reference (the `&` I added to the prototype), updating it in place without explicitly returning a new `vector`. When you accept by value and return by value, you're _constantly_ copy-constructing, doing a single `push_back` then returning and replacing the old `v`, causing a destruction of the old contents of `v` (and pre-C++11, causing yet another copy construction and destruction of the version inside the function as part of the return). – ShadowRanger Sep 30 '15 at 04:49
  • Oh my gosh, something as simple as one & made me get stuck for 1.5 hours. It makes sense though. Thank you!!!! – donnie Sep 30 '15 at 04:55
  • What I was doing was putting the dereference operator there, my mistake, because it still takes it to the value – donnie Sep 30 '15 at 05:22
1

You're never actually writing the second input into anything. Push_back is only called once (inside the function called at v = createtable(v, insertedstr);), so it will only contain the one value. You need to actually call push_back with the second value that is supposed to get into the vector.

dxdy
  • 540
  • 2
  • 13
0
vector<string> createtable(std::vector<std::string> v, std::string insertedstr) {
    //std::vector<std::string> vcreate; // Not required
    v.push_back(insertedstr + " ");

    return v;
}

Problem was with this function. You were creating new vector vcreate every time instead of using passed vector v. So you will have new vector of size 1 every time.

Nishant Kumar
  • 2,199
  • 2
  • 22
  • 43
  • How does triggering out of bounds access solve anything? That loop is perfectly fine, in that it doesn't access invalid indices, accessing `v[1]` when `v.size() == 1` guarantees invalid access. – ShadowRanger Sep 30 '15 at 04:18
  • @ShadowRanger i didn't mention but he should have a valid vector of size at least 2. – Nishant Kumar Sep 30 '15 at 04:20
  • Well, yeah. He doesn't have that, though, that's his whole problem, so saying "Once you solve the problem, you could hardcode an index that (if the problem is solved) doesn't even need hardcoding (because the `size` will go up and the loop will cover it seamlessly)" is not answering any question that has been asked. – ShadowRanger Sep 30 '15 at 04:23