vector push_back doesn't work c++

Lee M

Why is push_back not working as intended? Pretty confused as to why it doesn't work with my current code below

using namespace std;
void addItem(vector<string>& STRING, string item)
{
    STRING.push_back(item);
}

int main()
{
    string item;
    vector<string> STRING(100);
    ifstream myFile;
    myFile.open("food.txt");

    if (myFile.is_open()) //code where I store my data into the array
    {
        int i = 0;
        while (!myFile.eof())
        {
            getline(myFile, STRING[i]);
            i++;
        }
    }
    myFile.close();

    cin >> item;
    addItem(STRING, item);
    int x = 0;
    while(!STRING[x].empty()) //code to print the current array
    {
        cout << STRING[x];
        printf("\n");
        x++;
        return 0;
    }
}

Is there something wrong with how I initialized my array? Because when I used CodeBlocks, there were 0 errors and 0 warnings so I assumed it was fine until I ran it.

Kosmo

Your code does work. However you specified initial size of vector while creating it. Your vector starts with initial size of 100 elements. With that being said, you are indeed adding new element to the array, however push_back() is putting it right after already existing array - at 100th position.

You can avoid it by using defaul constructor

vector<string> STRING;

Also, I'll paste here my printList function, that will show you what the issue is with:

void printList(vector<string> &STRING)
{
    cout << "size: " << STRING.size() << '\n';
    for (int i = 0; i < STRING.size(); i++)
        cout << i << ":" << STRING[i] << ", ";
    cout << '\n';
}

@Edit: Fixed syntax error (Vector instead of vector). Thank you for pointing it out Christian Hackl!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ STD Vector push_back doesn't seem to work

From Java

std::vector::push_back() doesn't compile on MSVC for an object with deleted move constructor

From Dev

c++ push_back() a struct into a vector

From Dev

Implementing push_back(T&& c) in custom Vector<T> class

From Dev

vector copy does not work, but manual push_back does

From Dev

C++ Vector: push_back Objects vs push_back Pointers performance

From Dev

c++ vector - what's the difference between push_back(*new obj()) and push_back(obj())?

From Dev

Can't push_back a class into an object vector inside a for loop

From Dev

Why can't I push_back to a vector of const elements?

From Dev

Can't push_back an unique_ptr in a vector

From Dev

Can't push_back a class containing an ofstream pointer to a vector

From Dev

push_back/append or appending a vector with a loop in C++ Armadillo

From Dev

push_back objects into vector memory issue C++

From Dev

function that push_back value to vector in c++

From Dev

c++ no matching function for call to vector push_back

From Dev

function that push_back value to vector in c++

From Dev

C++: vector<Foo*> with reserve(), push_back() destroying values

From Dev

vector string push_back is not working in c++

From Dev

c++ - runtime error: vector push_back()

From Dev

vector resize doesn't work?

From Dev

sort on a vector doesn't work

From Dev

C++ std::function stored in vector doesn't work

From Dev

vector push_back in STL?

From Dev

Vector and push_back() behavior

From Dev

Unable to push_back to vector

From Dev

Vector cut plane in mayavi doesn't work

From Dev

Color selector for vector image doesn't work

From Dev

Erasing() an element in a vector doesn't work

From Dev

Vector cut plane in mayavi doesn't work

Related Related

  1. 1

    C++ STD Vector push_back doesn't seem to work

  2. 2

    std::vector::push_back() doesn't compile on MSVC for an object with deleted move constructor

  3. 3

    c++ push_back() a struct into a vector

  4. 4

    Implementing push_back(T&& c) in custom Vector<T> class

  5. 5

    vector copy does not work, but manual push_back does

  6. 6

    C++ Vector: push_back Objects vs push_back Pointers performance

  7. 7

    c++ vector - what's the difference between push_back(*new obj()) and push_back(obj())?

  8. 8

    Can't push_back a class into an object vector inside a for loop

  9. 9

    Why can't I push_back to a vector of const elements?

  10. 10

    Can't push_back an unique_ptr in a vector

  11. 11

    Can't push_back a class containing an ofstream pointer to a vector

  12. 12

    push_back/append or appending a vector with a loop in C++ Armadillo

  13. 13

    push_back objects into vector memory issue C++

  14. 14

    function that push_back value to vector in c++

  15. 15

    c++ no matching function for call to vector push_back

  16. 16

    function that push_back value to vector in c++

  17. 17

    C++: vector<Foo*> with reserve(), push_back() destroying values

  18. 18

    vector string push_back is not working in c++

  19. 19

    c++ - runtime error: vector push_back()

  20. 20

    vector resize doesn't work?

  21. 21

    sort on a vector doesn't work

  22. 22

    C++ std::function stored in vector doesn't work

  23. 23

    vector push_back in STL?

  24. 24

    Vector and push_back() behavior

  25. 25

    Unable to push_back to vector

  26. 26

    Vector cut plane in mayavi doesn't work

  27. 27

    Color selector for vector image doesn't work

  28. 28

    Erasing() an element in a vector doesn't work

  29. 29

    Vector cut plane in mayavi doesn't work

HotTag

Archive