Skip to content

Instantly share code, notes, and snippets.

@kazmura11
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kazmura11/a6acce63296165c2c801 to your computer and use it in GitHub Desktop.
Save kazmura11/a6acce63296165c2c801 to your computer and use it in GitHub Desktop.
How to use std::list
#include <iostream>
#include <list>
#include <algorithm>
#include <iterator>
int main()
{
std::list<int> ls { 12, 2, 3, 1, 5, 4, 13, 7, 8, 9, 10, 11, 6};
// とりあえず出力
std::copy(ls.begin(), ls.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
// 4を探す
auto iter = std::find(ls.begin(), ls.end(), 4);
int index = static_cast<int>(std::distance(ls.begin(), iter));
// 4がでるまで引きます
for (int i = 0; i < index; i++)
{
ls.pop_front();
}
std::copy(ls.begin(), ls.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment