C++迭代器最为参数传递出错

原问题:C++迭代器最为参数传递出错
分类:编程开发 > 最后更新时间:【2016-12-14 03:42:18】
问题补充:

#include<iostream>

#include<list>

using namespace std;

template<class T>

void exchange(list<T> &l1, typename list<T>::iterator p1, list<T> &l2, typename list<T>::iterator p2)

{

T temp;

while((p1 != l1.end()) && (p2 != l2.end()))

{

temp = *p1;

*p1 = *p2;

*p2 = temp;

p1++;

p2++;

}

}

void main()

{

int i;

list<int> l1, l2;

for(i=0; i<8; i++)

l1.push_back(i);

for(i=0; i<8; i++)

l2.push_back(i+8);

list<int>::iterator p1, p2;

p1 = l1.begin();

p2 = l2.begin();

exchange(l1, p1, l2, p2);

for(p1 = l1.begin(); p1 != l1.end(); p1++)

cout<<*p1<<' ';

cout<<endl;

for(p2 = l2.begin(); p2 != l2.end(); p2++)

cout<<*p2<<' ';

cout<<endl;

}

求解决,谢谢

最佳答案

如 void fun( std::vector<int>::iterator* pIter = NULL );
写个简单的例子:
#include <iostream>
#include <vector>
void f( std::vector<int>::iterator* pIter = NULL )
{
if( NULL == pIter ){
std::cout<< "invalid piter" << std::endl;
}
else{
std::cout<< *(*pIter) << std::endl;
}
}
void main()
{
std::vector<int> tmpList;
tmpList.push_back( 100 );
f(); // 默认参数 NULL
f( &tmpList.begin() );
}


  • 追问:
    能说说我的程序哪里错了吗,谢谢
    最佳答案由网友    提供
  • 公告: 为响应国家净网行动,部分内容已经删除,感谢网友理解。
    11

    分享到:

    其他回答

    暂无其它回答!

      推荐