forward_list是C++11规定的新标准单项链表,slist是g++以前的规定的单项链表

例程

#include<stdexcept>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<ctime>
#include<forward_list>
using namespace std; long get_a_target_long()
{
long target = 0;
cout<<"target(0~"<<RAND_MAX<<"):";
cin>>target;
return target;
}
string get_a_target_string()
{
long target = 0;
char buf[10];
cout<<"target(0~"<<RAND_MAX<<"):";
cin>>target;
snprintf(buf, 10, "%ld", target);
return string(buf);
}
int compareLongs(const void* a, const void* b)
{
return (*(long*)a - *(long*)b);
} int compareStrings(const void *a, const void *b)
{
if(*(string*)a > *(string*)b)
return 1;
else if(*(string*)a < *(string*)b)
return -1;
else
return 0;
}
void test_forward_list(long& value)
{
cout << "\ntest_forward_list().......... \n"; forward_list<string> c;
char buf[10]; clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.push_front(string(buf)); //由于效率原因,forward_list不提供push_back方法
}
catch(exception& p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "forward_list.max_size()= " << c.max_size() << endl;
cout << "forward_list.front()= " << c.front() << endl; string target = get_a_target_string();
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target);
cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl; if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl; timeStart = clock();
c.sort();
cout << "c.sort(), milli-seconds : " << (clock()-timeStart) << endl;
c.clear();
}
int main()
{
long int value;
cout<<"how many elements:";
cin>>value;
test_forward_list(value);
return 0;
}

运行效果

最新文章

  1. configuration error-could not load file or assembly crystaldecisions.reportappserver.clientdoc
  2. tyvj1938 最优战舰
  3. 查看Linux内存状况的命令及工具
  4. 深入剖析z-index属性
  5. [Aaronyang] 写给自己的WPF4.5 笔记19[Visual类图文并茂讲解]
  6. C语言+ODBC+SQL 操作(向SQL里面添加数据)
  7. vs 2013打开vs 2008解决方案问题解决
  8. 具体解释http 协议
  9. 基于visual Studio2013解决C语言竞赛题之1052求根
  10. springboot问题:解决异常Unable to start embedded container;
  11. SQLServer 索引总结
  12. What is volatile?
  13. 更新计算机名称后TFS文件无法迁出
  14. MySQL主从1205报错【转】
  15. 第二章 flex处理二义性
  16. Keepalived 配置高可用集群
  17. Java 多线程之 Thread 类 和 Runnable 接口初步使用
  18. SPSS-两变量相关性分析
  19. Aircrack-ng无线破解总结
  20. Java职业规划

热门文章

  1. python的分支结构
  2. ESXi 6.7 的https服务挂掉处理方法 503 Service Unavailable
  3. laravel常用查询
  4. Linux&amp;C open creat read write lseek 函数用法总结
  5. namespace之cgroup
  6. MySQL——DML数据增、删、改
  7. Solon &amp; Solon Cloud 1.5.62 发布,轻量级 Java 基础开发框架
  8. 基于霸道秉火的STM32F103ZET6嵌入式开发之------定时器中断3
  9. .net C# 释放内存 例子
  10. 菜鸡的Java笔记 - java 多对多映射