简体   繁体   中英

indexing names in a std::vector

I made a program that lets you enter your name and outputs it. I can't find a way to index my names. For example, if you enter "John, Jason, Jam", you would get the names numbered like so: "1:John, 2:Jason, 3:Jam"

int main()
{
    vector<string> names; 

    names.push_back(read_string("Enter a name: ")); 
    names.push_back(read_string("Enter a name: ")); 
    names.push_back(read_string("Enter a name: ")); 

    for(int  i = 0; i < names.size(); i++)
    {
            write_line( names[i]);
    }

    int total;
    total = total_length(names);

    write("Total length: ");
    write_line(total);

    bool has_John; 
    has_John = contains(names, "John");

    if ( has_John ) write_line("Contain's John");


    write_line(shortest_name(names));


    return 0;
}

I think you are looking for something like this:

 for(int  i = 0; i < names.size(); i++)
{
        write_line( (i+1) +names[i]);
}

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