容器vector
一、使用vector
#include <iostream>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <vector>
#include <string>
#include <stdexcept>
#include <algorithm>
#include "public_func.h"
using namespace std;
namespace Namespace_vector
{
void test_vector()
{
cout << "test_vector" << endl;
vector<string> vec;
char buf[10] = {0};
clock_t timestart = clock();
for (long i = 0; i < MAXSIZE; i++)
{
try
{
snprintf(buf, 10, "%d", rand());
vec.push_back(string(buf));
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
abort();
}
}
cout << "time(ms) = " << (clock() - timestart) << endl;
cout << "vec.size() = " << vec.size() << endl;
cout << "vec.front() = " << vec.front() << endl;
cout << "vec.back() = " << vec.back() << endl;
cout << "vec.data() = " << vec.data() << endl;
cout << "vec.capacity() = " << vec.capacity() << endl;
string target = get_a_string_target();
timestart = clock();
{
auto pItem = ::find(vec.begin(), vec.end(), target);
cout << "::find(), time = " << (clock()-timestart) << endl;
if (pItem != vec.end())
{
cout << "found, " << *pItem << endl;
}
else
{
cout << "not found!" << endl;
}
}
{
timestart = clock();
sort(vec.begin(), vec.end());
string* pItem = (string*)bsearch(&target, vec.data(), vec.size(), sizeof(string), compareStrings);
cout << "qsort + bsearch, time(ms) = " << (clock()-timestart) << endl;
if (pItem != NULL)
{
cout << "found, " << *pItem << endl;
}
else
{
cout << "not found" << endl;
}
}
}
}
int main(int argc, char *argv[])
{
cout << "----------STL VECTOR BEGIN------------\n" << endl;
Namespace_vector::test_vector();
cout << "\n-----------STL VECTOR END-----------" << endl;
system("pause");
return 0;
}
二、vector源码解析
template<class T, class Alloc = alloc>
class vector{
typedef T value_type;
typedef value_type* iterator;
typedef value_type& reference;
typedef size_t size_type;
iterator start;
iterator finish;
iterator end_of_storage;
iterator begin(){return start;}
iterator end(){return finish;}
size_type size() const {end() - begin();}
size_type capacity() const() {end_of_storage() - begin();}
bool empty() const {return begin() == end();}
reference operator[](size_type n){return *(begin() + n);}
reference front() {return *begin();};
reference back() {return * (end()-1);}
};
三、vector内存扩充
void push_back(const T& x){
if (finish != end_of_storage){
construct(finish, x);
++finish;
}
else
insert_aux(end(), x);
}
template<class T, class Alloc>
void vector<T,Alloc>::insert_aux(iterator position, const T& x){
if (finish != end_of_storage){
construct(finish, *(finish-1));
++finish;
T x_copy = x;
copy_backward(position, finish-2, finish-1);
*position = x_copy;
}
else{
const size_type old_size = size();
const size_type len = old_size ? 0 2*old_size : 1;
iterator new_start = data_allocator::allocate(len);
iterator new_finish = new_start;
try{
new_finish = uninitialized_copy(start, position, new_start);
++new_finish;
new_finish = uninitialized_copy(position, finish, new_finish);
} catch(...){
destroy(new_start, new_finish);
data_allocator::deallocate(new_start, len);
throw;
}
destroy(begin(), end());
deallocate();
start = new_start;
finish = new_finish;
end_of_storage = new_start + len;
}
}
四、vector迭代器iterator
template<class T, class Alloc=alloc>
class vector{
typedef T value_type;
typedef value_type* iterator;
};