您的位置:首页 > 编程语言 > C语言/C++

c++ forward_list 的使用

2017-12-18 20:27 225 查看
/*C++11 forward_list的使用*/

bool signal(const int&v)
{
if (v > 10 && v < 30)
{
return true;
}
return false;
}

bool signal2(const int&v1,const int &v2)
{
if (v1 > v2)
{
return true;
}
return false;
}

void main5_6()
{
int arr[] = { 2, 3, 4, 5 };
int size = sizeof(arr) / sizeof(int);

std::forward_list<int> myList(arr, arr + size);

auto it = myList.before_begin();//指向头一个元素的前一个位置,这个是单链表,方便插入数据
it++;
while (it != myList.end())
{

cout <<*it<< endl;
it++;
}

if (myList.empty())
{
cout << "Empty 1" << endl;
}
else
{
cout << "Not Empty 1" << endl;
}

forward_list<int>::reference re = myList.front();// 返回第一个元素的引用;

cout << re << endl;
re = 22;
it = myList.begin();
cout << *it << endl;

//myList.~forward_list();   //销毁整个list

cout <<" *****" << endl;
myList.push_front(12); //在头部添加一个元素
////myList.pop_front();

it = myList.before_begin();
myList.insert_after(it, 99); //在迭代器后面开始添加元素

//myList.insert_after(it, arr, arr + size); //插入数组的元素
//myList.insert_after(it, 10, 100);//插入10个值为100的元素

it = myList.begin();
//myList.erase_after(it);  //删除it位置的元素
auto it2 = myList.end();
/*it2++;
it2++;
it2++;*/

//myList.erase_after(it, it2); //删除it下一个元素到it2上一个元素之间的所有元素

myList.remove(99); //删除值为99的元素

//myList.reverse();  //所有元素反序
myList.sort();   //所有元素重新排序,默认是升序
//myList.sort(signal2); //从大道小排序

/*myList.remove_if(signal);*/

std::forward_list<int> myList2 = {7,100,111};//要保证也是降序,

myList2.sort();

myList.merge(myList2);  //只支持升序
it = myList.begin();
while (it != myList.end())
{
cout << *it << endl;
it++;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: