网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
using std::cout;
using std::endl;
using std::string;
using std::array;
using std::vector;
using std::list;
using std::forward_list;
using std::deque;
using std::stack;
using std::queue;
using std::pair;
using std::set;
using std::map;
using std::multiset;
using std::multimap;
using std::unordered_set;
using std::unordered_map;
using std::unordered_multiset;
using std::unordered_multimap;
using std::exception;
const long ASIZE = 250000L; //50万
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,"%d",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;
}
namespace pp01
{
void test_array(const long& value)
{
cout << "test\_array()......................\n";
array<long, ASIZE> c;
clock_t timeStart = clock();
srand((unsigned)time(NULL)); //种子,产生随机
for (long i = 0; i < value; ++i)
{
try
{
c[i] = rand();
}
catch (exception* p)
{
cout << "i = " << i << " " << p->what() << endl;
abort();
}
}
cout << "front: " << c[0] << endl; //第一个array容器中的数值
cout << "milli-seconds: " << (clock() - timeStart) << endl; //存放25000个数需要的时间
cout << "array.size() = " << c.size() << endl;//array的大小
cout << "array.front() = " << c.front() << endl; //第一个array容器中的数值
cout << "array.back() = " << c.back() << endl; //最后一个array容器中的数值
cout << "array.data() = " << c.data() << endl; //array容器的地址
cout << "array.begin() = " << c.begin() << endl; //array容器的地址
long target = get_a_target_long();
timeStart = clock();
::qsort(c.data(), c.size(), sizeof(long), compareLongs); //c排序方法
long* pItem = (long*)::bsearch(&target, c.data(), c.size(), sizeof(long), compareLongs); //c查找的方法
cout << "qsort() + bsearch(), milli-seconds: " << (clock() - timeStart) << endl;
if (pItem != NULL)
cout << "found, " << *pItem << endl;
else
cout << "not found!" << endl;
}
}
namespace pp02
{
//vector二倍内存扩展
void test_vector(const long& value)
{
cout << “\ntest_vector()… \n”;
vector 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;
//曾经最高 i=58389486 then std::bad_alloc
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "vector.max_size()= " << c.max_size() << endl; //1073747823
cout << "vector.size()= " << c.size() << endl;
cout << "vector.front()= " << c.front() << endl;
cout << "vector.back()= " << c.back() << endl;
cout << "vector.data()= " << c.data() << endl;
cout << "vector.capacity()= " << c.capacity() << endl << endl;
string target = get_a_target_string();
{
//方法1: 使用算子函数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 << endl;
else
cout << "not found! " << endl << endl;
}
{
//方法2: 排序+二分查找
timeStart = clock();
sort(c.begin(), c.end());
cout << "sort(), milli-seconds : " << (clock()-timeStart) << endl;
timeStart = clock();
string* pItem = (string*)::bsearch(&target, (c.data()),
c.size(), sizeof(string), compareStrings);
cout << "bsearch(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != NULL)
cout << "found, " << *pItem << endl << endl;
else
cout << "not found! " << endl << endl;
}
c.clear();
}
}
namespace pp03
{
void test_list(const long& value)
{
cout << “\ntest_list()… \n”;
list 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();
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.sort();
cout << "c.sort(), milli-seconds : " << (clock()-timeStart) << endl;
c.clear();
}
}
namespace pp04
{
void test_forward_list(const long& value)
{
cout << “\ntest_forward_list()… \n”;
forward_list 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();
//自己的sort
c.sort();
cout << "c.sort(), milli-seconds : " << (clock()-timeStart) << endl;
c.clear();
}
}
namespace pp05
{
void test_slist(const long& value)
{
cout << “\ntest_slist()… \n”;
__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.push_front(string(buf));
}
catch(exception& p) {
cout << "i=" << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
}
}
namespace pp06
{
void test_deque(const long& value)
{
cout << “\ntest_deque()… \n”;
deque 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
sort(c.begin(), c.end());
cout << "sort(), milli-seconds : " << (clock()-timeStart) << endl;
c.clear();
}
}
namespace pp07
{
void test_stack(const long& value)
{
cout << “\ntest_stack()… \n”;
stack c; //以 deque为底层机制
char buf[10];
clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try {
snprintf(buf, 10, “%d”, rand());
c.push(string(buf));
}
catch(exception& p) {
cout << “i=” << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "stack.size()= " << c.size() << endl;
cout << "stack.top()= " << c.top() << endl;
c.pop();
cout << "stack.size()= " << c.size() << endl;
cout << "stack.top()= " << c.top() << endl;
{
stack<string, list> c; //以 list为底层机制
for(long i=0; i< 10; ++i) {
snprintf(buf, 10, “%d”, rand());
c.push(string(buf));
}
cout << "stack.size()= " << c.size() << endl;
cout << "stack.top()= " << c.top() << endl;
c.pop();
cout << "stack.size()= " << c.size() << endl;
cout << "stack.top()= " << c.top() << endl;
}
{
stack<string, vector<string>> c; //以 vector为底层机制
for(long i=0; i< 10; ++i) {
snprintf(buf, 10, "%d", rand());
c.push(string(buf));
}
cout << "stack.size()= " << c.size() << endl;
cout << "stack.top()= " << c.top() << endl;
c.pop();
cout << "stack.size()= " << c.size() << endl;
cout << "stack.top()= " << c.top() << endl;
}
}
}
namespace pp08
{
void test_queue(const long& value)
{
cout << “\ntest_queue()… \n”;
queue c;
char buf[10];
clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try {
snprintf(buf, 10, “%d”, rand());
c.push(string(buf));
}
catch(exception& p) {
cout << “i=” << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "queue.size()= " << c.size() << endl;
cout << "queue.front()= " << c.front() << endl;
cout << "queue.back()= " << c.back() << endl;
c.pop();
cout << "queue.size()= " << c.size() << endl;
cout << "queue.front()= " << c.front() << endl;
cout << "queue.back()= " << c.back() << endl;
{
queue<string, list> c; //以 list为底层机制
for(long i=0; i< 10; ++i) {
snprintf(buf, 10, “%d”, rand());
c.push(string(buf));
}
cout << "queue.size()= " << c.size() << endl;
cout << "queue.front()= " << c.front() << endl;
cout << "queue.back()= " << c.back() << endl;
c.pop();
cout << "queue.size()= " << c.size() << endl;
cout << "queue.front()= " << c.front() << endl;
cout << "queue.back()= " << c.back() << endl;
}
}
}
namespace pp09
{
void test_multiset(const long& value)
{
cout << “\ntest_multiset()… \n”;
multiset c;
char buf[10];
clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try {
snprintf(buf, 10, “%d”, rand());
c.insert(string(buf));
}
catch(exception& p) {
cout << “i=” << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "multiset.size()= " << c.size() << endl;
cout << "multiset.max_size()= " << c.max_size() << endl; //214748364
string target = get_a_target_string();
{
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target); //比 c.find(…) 慢很多
cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
}
{
timeStart = clock();
auto pItem = c.find(target); //比 std::find(…) 快很多
cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
}
c.clear();
}
}
namespace pp10
{
void test_multimap(const long& value)
{
cout << “\ntest_multimap()… \n”;
multimap<long, string> c;
char buf[10];
clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try {
snprintf(buf, 10, “%d”, rand());
//multimap 不可使用 [] 做 insertion
c.insert(pair<long,string>(i,buf));
}
catch(exception& p) {
cout << “i=” << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "multimap.size()= " << c.size() << endl;
cout << "multimap.max_size()= " << c.max_size() << endl; //178956970
long target = get_a_target_long();
timeStart = clock();
//容器的find
auto pItem = c.find(target);
cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << “found, value=” << (*pItem).second << endl;
else
cout << "not found! " << endl;
c.clear();
}
}
namespace pp11
{
void test_unordered_multiset(const long& value)
{
cout << “\ntest_unordered_multiset()… \n”;
unordered_multiset c;
char buf[10];
clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try {
snprintf(buf, 10, “%d”, rand());
c.insert(string(buf));
}
catch(exception& p) {
cout << “i=” << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "unordered_multiset.size()= " << c.size() << endl;
cout << "unordered_multiset.max_size()= " << c.max_size() << endl; //357913941
cout << "unordered_multiset.bucket_count()= " << c.bucket_count() << endl;
cout << "unordered_multiset.load_factor()= " << c.load_factor() << endl;
cout << "unordered_multiset.max_load_factor()= " << c.max_load_factor() << endl;
cout << “unordered_multiset.max_bucket_count()= " << c.max_bucket_count() << endl;
for (unsigned i=0; i< 20; ++i) {
cout << “bucket #” << i << " has " << c.bucket_size(i) << " elements.\n”;
}
string target = get_a_target_string();
{
timeStart = clock();
auto pItem = find(c.begin(), c.end(), target); //比 c.find(…) 慢很多
cout << "std::find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
}
{
timeStart = clock();
auto pItem = c.find(target); //比 std::find(…) 快很多
cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << "found, " << *pItem << endl;
else
cout << "not found! " << endl;
}
c.clear();
}
}
namespace pp12
{
void test_unordered_multimap(const long& value)
{
cout << “\ntest_unordered_multimap()… \n”;
unordered_multimap<long, string> c;
char buf[10];
clock_t timeStart = clock();
for(long i=0; i< value; ++i)
{
try {
snprintf(buf, 10, “%d”, rand());
//multimap 不可使用 [] 進行 insertion
c.insert(pair<long,string>(i,buf));
}
catch(exception& p) {
cout << “i=” << i << " " << p.what() << endl;
abort();
}
}
cout << "milli-seconds : " << (clock()-timeStart) << endl;
cout << "unordered_multimap.size()= " << c.size() << endl;
cout << "unordered_multimap.max_size()= " << c.max_size() << endl; //357913941
long target = get_a_target_long();
timeStart = clock();
auto pItem = c.find(target);
cout << "c.find(), milli-seconds : " << (clock()-timeStart) << endl;
if (pItem != c.end())
cout << “found, value=” << (*pItem).second << endl;
else
cout << "not found! " << endl;
}
}
/
namespace pp13
{
void test_set(const long& value)
{
cout << “\ntest_set()… \n”;
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
if (pItem != c.end())
cout << "found, value=" << (*pItem).second << endl;
else
cout << "not found! " << endl;
}
}
/
namespace pp13
{
void test_set(const long& value)
{
cout << “\ntest_set()… \n”;
[外链图片转存中…(img-lK6zt1pg-1715697572895)]
[外链图片转存中…(img-SAfuOKg1-1715697572895)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!