Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用列表作为索引打印另一个列表中的某些元素 我有这个问题,我目前正在C++中工作。_C++_List_Stl - Fatal编程技术网

使用列表作为索引打印另一个列表中的某些元素 我有这个问题,我目前正在C++中工作。

使用列表作为索引打印另一个列表中的某些元素 我有这个问题,我目前正在C++中工作。,c++,list,stl,C++,List,Stl,您将得到一个列表L和另一个列表p,其中包含按升序排序的整数 顺序printLots(L,P)操作将打印L中处于位置的元素 由P指定。例如,如果P=1、3、4、6,则位置1、3、4和6中的元素 在L中打印。编写程序printLots(L,P)。你可以只使用公众 STL容器操作 这是我目前掌握的代码 void printLots(list<int> L, list<int> P) { list<int>::iterator myListIterator;

您将得到一个列表L和另一个列表p,其中包含按升序排序的整数 顺序printLots(L,P)操作将打印L中处于位置的元素 由P指定。例如,如果P=1、3、4、6,则位置1、3、4和6中的元素 在L中打印。编写程序printLots(L,P)。你可以只使用公众 STL容器操作

这是我目前掌握的代码

void printLots(list<int> L, list<int> P)
{
    list<int>::iterator myListIterator;

    for (myListIterator = P.begin();
        myListIterator != P.end();
         ++myListIterator)
    {
        //Not sure what to do here
    }
}
作废打印件(列表L、列表P)
{
列表::迭代器myListIterator;
for(myListIterator=P.begin();
myListIterator!=P.end();
++MyListerator)
{
//我不知道该怎么办
}
}

我知道我可以很容易地打印列表p的内容,但我不知道如何使用它作为索引来打印列表L中那些位置的元素。根据我的研究,也没有办法直接索引列表,所以我不太确定从这里开始该怎么做。

假设使用std::List是一项要求(从您的问题来看不确定)然后,如Karoly在评论中所述,使用计数器跟踪您在链接列表中的位置

void printLots(const std::list<int>& L, const std::list<int>& P)
{
  std::list<int>::const_iterator it;
  for (it = P.begin(); it != P.end(); ++it) 
  {
    int indexRef = *it;
    // You never know
    if (indexRef < 0 || indexRef >= L.size()) continue;
    std::list<int>::const_iterator it2;
    int cntr = 0;
    for (it2 = L.begin(); it2 != L.end(); ++it2)
    {
      if (cntr == indexRef)
      {
        std::cout << *it2 << std::endl;
        break;
      }
      cntr++;
    }
  }
}
void打印批次(常量标准::列表和列表,常量标准::列表和P)
{
std::list::const_迭代器it;
for(it=P.begin();it!=P.end();+it)
{
int indexRef=*it;
//你永远不知道
如果(indexRef<0 | | indexRef>=L.size())继续;
std::list::const_迭代器it2;
int-cntr=0;
对于(it2=L.begin();it2!=L.end();+it2)
{
如果(cntr==indexRef)
{
std::cout这里有一个想法:

#include <list>
#include <iostream>

typedef std::list<int> list_of_int;

void print( const list_of_int& L, const list_of_int& P )
{
  int p = 0;
  for ( list_of_int::const_iterator i = L.begin(), j = P.begin(); i != L.end(); ++i, ++p )
  {
    if ( p == *j )
    {
      std::cout << *i << ' ';
      ++j;
    }
  }
}

int main()
{
  list_of_int 
  L = {0, 10, 20, 30, 40, 50, 60}
  , P = {2, 4, 5};
  print( L, P );
  return 0;
}
#包括
#包括
typedef std::list of int;
无效打印(常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表,常量列表)
{
int p=0;
for(list_of_int::const_迭代器i=L.begin(),j=P.begin();i!=L.end();++i,++P)
{
如果(p==*j)
{

很抱歉,我不确定这会有什么帮助。你能解释一下吗?这不会很有效,是吗?O(n)实现可能吗?