2021-08-18 list j接口1

// vector stl 8.17.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。
//

//#include
//
//int main()
//{
// std::cout << “Hello World!\n”;
//}

// 运行程序: Ctrl + F5 或调试 >“开始执行(不调试)”菜单
// 调试程序: F5 或调试 >“开始调试”菜单

// 入门使用技巧:
// 1. 使用解决方案资源管理器窗口添加/管理文件
// 2. 使用团队资源管理器窗口连接到源代码管理
// 3. 使用输出窗口查看生成输出和其他消息
// 4. 使用错误列表窗口查看错误
// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目
// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件
//#include
//using namespace std;
//#include
//#include
//class String
//{
//public:
// String(const char* str =" " )
// :_ptr(new char[strlen(str)])
// {
// strcpy(_ptr, str);
// }
// ~String()
// {
// if (_ptr)
// delete[] _ptr;
// _ptr = nullptr;
//
// }
//private:
// char* _ptr;
//};
//void test()
//{
// vector v;
// v.push_back(“123”);
// v.push_back(“123”);
// v.push_back(“123”);
// v.push_back(“123”);
// v.push_back(“123”);
//
//}
//int main()
//{
// test();
// return 0;
//}

list 接口的实现
//#include
//#include
//using namespace std;
//#include
//void test()
//{
// //类似之前的容器是使用的是默认构造。
// list lst;
// list lst2;
//
// list lst3(3, 5);
// string str = “123456”;
// list lst4(str.begin(), str.end());
// char arr[] = “abcdefg”;
// list lst5(arr, arr + sizeof(arr) / sizeof(arr[0]));
//
// vector v(str.begin(), str.end());
// list lst6(v.begin(), v.end());
//
//}
//
//void test()
//{
// string s = “2345”;
// list lst(s.begin(), s.end());
// //注意迭代器的使用方式
// list::iterator it = lst.begin();
// while (it != lst.end())
// {
// cout << *it << " ";
// *it = ‘a’;
// ++it;
// }
// cout << endl;
//
// list::const_iterator cit = lst.cbegin();
// while (cit != lst.cend())
// {
// cout << *it << endl;
// //只读迭代器
// //*cit=‘b’;
// ++cit;
// }
// cout << endl;
//}
//int main()
//{
// test();
// return 0;
//
//}

constructing lists
//#include
//#include
//using namespace std;
//int main()
//{
// //constructors used in the same order as described above:
// //default (1)
// //explicit list(const allocator_type& alloc = allocator_type());
// //fill(2)
// //explicit list(size_type n, const value_type& val = value_type(),
// // const allocator_type& alloc = allocator_type());
// //range(3)
// //template
// //list(InputIterator first, InputIterator last,
// // const allocator_type& alloc = allocator_type());
// //copy(4)
// // list(const list& x);
// //使用输出运算符的重载
// list first; // empty list of ints
// cout << “first” << endl;
// list second(4, 100); // four ints with value 100
// cout << “second” << endl;
// list third(second.begin(), second.end()); // iterating through second
// cout << “third” << endl;
// list fourth(third); // a copy of third
// cout << “fourth” << endl;
//
// // the iterator constructor can also be used to construct from arrays:
// int myints[] = { 16,2,77,29 };
// list fifth(myints, myints + sizeof(myints) / sizeof(int));
// cout << “fifth” << endl;
// cout << "The contents of fifth are: ";
// for (list::iterator it = fifth.begin(); it != fifth.end(); it++)
// cout << *it << ’ ';
// cout << ‘\n’;
// return 0;
//}

constructing lists
//#include
//#include
//using namespace std;
//int main()
//{
// std::list l1; // 构造空的l1
// std::list l2(4, 100); // l2中放4个值为100的元素
// std::list l3(l2.begin(), l2.end()); // 用l2的[begin(), end())左闭右开的区间构造l3
// std::list l4(l3); // 用l3拷贝构造l4
// // 以数组为迭代器区间构造l5
// int array[] = { 16,2,77,29 };
// std::list l5(array, array + sizeof(array) / sizeof(int));
// // 用迭代器方式打印l5中的元素
// for (std::list::iterator it = l5.begin(); it != l5.end(); it++)
// std::cout << *it << " ";
// std::cout << endl;
// // C++11范围for的方式遍历 范围for就是指的是一定是在范围确定的情况下
// for (auto& e : l5)
// std::cout << e << " ";
// std::cout << endl;
// return 0;
//}

//#include
//using namespace std;
//#include
//void print_list(const list& l)
//{
// // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
// for (list::const_iterator it = l.begin(); it != l.end(); ++it)
// {
// cout << *it << " ";
// // *it = 10; 编译不通过
// }
//
// cout << endl;
//}
//int main()
//{
// int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
// list l(array, array + sizeof(array) / sizeof(array[0]));
// // 使用正向迭代器正向list中的元素
// for (list::iterator it = l.begin(); it != l.end(); ++it)
// cout << *it<< " ";
//
// cout << endl;
// // 使用反向迭代器逆向打印list中的元素
// for (list::reverse_iterator it = l.rbegin(); it != l.rend(); ++it)
// cout << *it << " ";
// cout << endl;
// return 0;
//}

list::empty
//#include
//#include
//using namespace std;
//int main()
//{
// list mylist;
// int sum(0);
// for (int i = 1; i <= 10; ++i)
// {
// mylist.push_back(i);
// }
// while (!mylist.empty())
// {
// sum += mylist.front();
// mylist.pop_front();
// }
// cout << "total: " << sum << ‘\n’;
// return 0;
//}

// list::size
#include
#include
using namespace std;
int main()
{
list myints;
cout << "0. size: " << myints.size() << ‘\n’;
for (int i = 0; i < 10; i++)
{
myints.push_back(i);
}
cout << "1. size: " << myints.size() << ‘\n’;
myints.insert(myints.begin(), 10, 100);
cout << "2. size: " << myints.size() << ‘\n’;
myints.pop_back();
cout << "3. size: " << myints.size() << ‘\n’;
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值