其他未完成
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(const list<T> &lst)
|
||||
{
|
||||
for (typename list<T>::const_iterator it = lst.begin(); it != lst.end(); ++it)
|
||||
cout << *it << ' ';
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
list<int> lst1(6, 5); // 6 个 5
|
||||
print(lst1);
|
||||
|
||||
string s1 = "abcdefg";
|
||||
|
||||
// 将 string 字符串转换为 list
|
||||
list<char> lst2(s1.begin(), s1.end());
|
||||
print(lst2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(const list<T> &lst)
|
||||
{
|
||||
for (typename list<T>::const_iterator it = lst.begin(); it != lst.end(); ++it)
|
||||
cout << *it << ' ';
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string s = "abcdefg";
|
||||
|
||||
// 将 string 字符串转换为 list
|
||||
list<char> l(s.begin(), s.end());
|
||||
int size = l.size(); // 获取大小
|
||||
print(l);
|
||||
|
||||
// list 手写逆序
|
||||
list<char>::iterator it = l.begin(); // 迭代器指向第一个元素
|
||||
for (int i = 0; i < size; i++) // 逆序 size 次
|
||||
{
|
||||
it = l.insert(it, l.back()); // 将最后一个元素插入到第一个元素之前
|
||||
it++; // 迭代器指向下一个元素
|
||||
l.pop_back(); // 删除最后一个元素
|
||||
}
|
||||
|
||||
print(l);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(const list<T> &lst)
|
||||
{
|
||||
for (typename list<T>::const_iterator it = lst.begin(); it != lst.end(); ++it)
|
||||
cout << *it << ' ';
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string s = "abcdefg";
|
||||
list<char> l(s.begin(), s.end());
|
||||
|
||||
// 删除 cde
|
||||
// list<char>::iterator it = find(l.begin(), l.end(), 'c');
|
||||
// l.erase(it, find(l.begin(), l.end(), 'e'));
|
||||
|
||||
list<char>::iterator it = l.begin(); // 迭代器指向第一个元素
|
||||
for (int i = 0; i < 2; i++) // 迭代器指向第三个元素
|
||||
it++;
|
||||
list<char>::iterator it2 = it; // 迭代器指向第三个元素
|
||||
for (int i = 0; i < 3; i++) // 迭代器指向第六个元素
|
||||
it2++;
|
||||
l.erase(it, it2); // 删除第三个到第五个元素
|
||||
|
||||
print(l); // a b f g
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <iostream>
|
||||
#include <list>
|
||||
#include <algorithm>
|
||||
|
||||
using namespace std;
|
||||
|
||||
template <typename T>
|
||||
void print(const list<T> &lst)
|
||||
{
|
||||
for (typename list<T>::const_iterator it = lst.begin(); it != lst.end(); ++it)
|
||||
cout << *it << ' ';
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string s = "abcdefg";
|
||||
list<char> l(s.begin(), s.end());
|
||||
print(l);
|
||||
l.reverse(); // 逆序
|
||||
print(l);
|
||||
|
||||
int m[] = {6, 4, 2, 0, 8, 6, 4};
|
||||
list<int> l2(m, m + 7);
|
||||
print(l2);
|
||||
l2.sort(); // 排序
|
||||
print(l2);
|
||||
l2.reverse(); // 逆序
|
||||
print(l2);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user