C++ 按索引C+打印字符串列表中的元素+;
嗨,这是我到目前为止所拥有的。操作printList(strList,indexList)将打印strList中位于indexList指定位置的元素,但我在这样做时遇到了麻烦。我试图使用公共STL容器操作,但遇到了问题。任何帮助都会很好!谢谢大家!C++ 按索引C+打印字符串列表中的元素+;,c++,C++,嗨,这是我到目前为止所拥有的。操作printList(strList,indexList)将打印strList中位于indexList指定位置的元素,但我在这样做时遇到了麻烦。我试图使用公共STL容器操作,但遇到了问题。任何帮助都会很好!谢谢大家! #include<cstdlib> #include<iostream> #include<string> #include<list> using namespace std; // PURPOS
#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);}
#包括
#包括
#包括
#包括
使用名称空间std;
//目的:让用户在列表“strList”中输入字符串。没有返回值。
无效enterStringList(列表和strList){
while(true){
字符串输入;
不欢迎使用堆栈溢出。您的问题似乎是您不知道如何编写printList
函数;您编写的代码与问题无关。这是一个帮助论坛,不是免费的代码编写服务。我建议您尝试编写一个打印列表中一个元素的函数,并从中构建。请尝试以下方法:方法:将你的橡皮鸭放在键盘旁边,然后用通俗易懂的英语向你的橡皮鸭解释你提出的实现函数的算法。用你自己的话将你的解释表述为一个逻辑的、一步一步的过程。当你最终说服你的橡皮鸭你的算法会成功rk,你的橡皮鸭同意,然后把你冗长的解释翻译成代码。简单。