通过索引C ++打印字符串列表中的元素

时间:2016-02-21 00:17:14

标签: c++

嗨,这是我到目前为止所拥有的。操作printList(strList,indexList)将打印strList中的元素,这些元素位于indexList指定的位置,但是我很难这样做。我正在尝试使用公共STL容器操作,但遇到了麻烦。任何帮助都会很棒!谢谢!

#include<cstdlib>
#include<iostream>
#include<string>
#include<list>
using namespace std;

//  PURPOSE:  To have the user enter strings into list 'strList'.  No return value.
void enterStringList(list<string>&strList){
while (true){
    string  entry;
    cout << "Please enter a string or just press 'enter' to quit: ";
    getline(cin, entry);

    if (entry.empty())
        break;

    strList.push_back(entry);
}
}

void enterIntegerList(list<int>& intList, int limit) {
    bool shouldContinue = true;

while (shouldContinue)
{
    int     number;

    do
    {
        string  entry;

        cout << "Please enter an integer [0-"
            << limit
            << "], or just press 'enter' to quit: ";
        getline(cin, entry);

        if (entry.empty())
        {
            shouldContinue = false;
            break;
        }

        number = atoi(entry.c_str());
    } while ((number < 0) || (number > limit));

    if (shouldContinue)
        intList.push_back(number);
}
}


//  PURPOSE:  To print the elements in L that are in positions specified by P.
//  No return value.
void printList(list<string>& strList, list<int>& indexList){
//  HERE }

int     main() {
list<string>    strList;
list<int>   indexList;

enterStringList(strList);

if (!strList.empty())
{
    enterIntegerList(indexList, strList.size() - 1);
    indexList.sort();
    printList(strList, indexList);
}
return(EXIT_SUCCESS);}

0 个答案:

没有答案