C-Standard-Library-architecure & sources2

list容器(链表)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <list>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <algorithm> //find()
#include <iostream>
#include <ctime>
namespace jj03
{
void test_list(long& value)
{
cout << "\ntest_list().......... \n";

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_back(string(buf));
}
catch(exception& p) {
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "list.size()= " << c.size() << endl;
cout << "list.max_size()= " << c.max_size() << endl; //357913941
cout << "list.front()= " << c.front() << endl;
cout << "list.back()= " << c.back() << endl;


string target = get_a_target_string();
//-----------find----------------------
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;
//-----------sort----------------------
timeStart = clock();
c.sort();
cout << "c.sort(), milli-seconds : " << (clock()-timeStart) << endl;

c.clear();
test_moveable(list<MyString>(),list<MyStrNoMove>(), value);
}
}
  • 一个萝卜一个坑
  • 标准库有sort,容器自己也有一个sort,容器查找自然要用自己的sort

forward_list容器——单向链表(c++11)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <forward_list>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <iostream>
#include <ctime>
namespace jj04
{
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));
}
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; //536870911
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();
}
}
  • 只有put_front( ) 没有put_back( )
  • 单向链表找到最后一个元素非常慢
  • .size( )和.back( )都是不存在的

slist容器(GNU,非标准)等价于forward_list

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <ext\slist>
#include <stdexcept>
#include <string>
#include <cstdlib>
#include <cstdio>
#inlcude <iostream>
#include <ctime>

namespace jj10
{
void test_slist(long& value)
{
__gnu_cxx::slist<string> c;
char buf[10];

clock_t timeStart = clock();
for (long i = 0;i< value; ++i)
{
try
{
snprintf(buf, 10, "%d", rand());
c.put_front(string(buf));
}
catch(exception& p)
{
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
}
}

deque容器——双端队列:双向进出

AkBPh9.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <deque>
#include <stdexcept>
#include <string>
#include <cstdlib> //abort()
#include <cstdio> //snprintf()
#include <iostream>
#include <ctime>
namespace jj05
{
void test_deque(long& value)
{
cout << "\ntest_deque().......... \n";

deque<string> c;
char buf[10];

clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try {
snprintf(buf, 10, "%d", rand());
c.push_back(string(buf));
}
catch(exception& p) {
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "deque.size()= " << c.size() << endl;
cout << "deque.front()= " << c.front() << endl;
cout << "deque.back()= " << c.back() << endl;
cout << "deque.max_size()= " << c.max_size() << endl; //1073741821

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();
sort(c.begin(), c.end());
cout << "sort(), milli-seconds : " << (clock()-timeStart) << endl;

c.clear();
test_moveable(deque<MyString>(),deque<MyStrNoMove>(), value);
}
}

关于stack和queue

  • stack栈(先进后出)和queue队列(先进先出)其实是deque的容器适配器,但是也可以称之为容器
  • 都是.push()
  • 不提供iterator的操作,没有find算法,会破坏二者的特性
-------------本文结束感谢您的阅读-------------

本文标题:C-Standard-Library-architecure & sources2

文章作者:孤岛violet

发布时间:2019年03月13日 - 19:27

最后更新:2019年03月14日 - 14:33

原始链接:http://yoursite.com/2019/03/13/C-Standard-Library-architecure-sources2/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

undefined