简体   繁体   中英

C++ - Gettings size from vector in FOR loop

I have a loop that asks for user input, and adds it too a vector, then when if they type "EXIT" it will stop and display the list. What I am trying to do now is determine the number of elements with size()

This is what I have:

#include <iostream>
#include <string>
#include <unistd.h>
#include <iterator>
#include <vector>
using namespace std;

int main()
{
    write(1,"\E[H\E[2J",7);                                  
    vector<string> list;
    cout << "Enter UIDs: \n\n"; 
    for(string uid ; cin >> uid && uid != "EXIT"; list.push_back(uid))
        cout << " \n";
    copy(list.begin(), list.end(), ostream_iterator<string>(cout, "\n\n"));
    cout << "Vector size: " << uid.size() << endl;
    return 0;
}

When attempting to compile that I get the error:

g++ sof.cpp -o sof

sof.cpp: In function 'int main()':
sof.cpp:16:32: error: name lookup of 'uid' changed for ISO 'for' scoping
sof.cpp:13:16: error: cannot use obsolete binding at 'uid' because it has a destructor

你在查询uid.size()而不是list.size()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM