假期无聊,想到制作自己的列表类型的类,并使用迭代器和所有其他有趣的东西
到目前为止,我的代码(仅是该问题感兴趣的部分):
template <typename T> class my_list {
//Code about iterators with operator overload constructors and yada yada yada...
//shared_pointer is a typedef of shared_ptr<node_type>
//My 2 iterators (begin and end) and the number of elements
t_iterator it_begin;
t_iterator it_end;
int n_elements = 0;
public:
void push_back(const T& _data) {
cout << "Create\n";
shared_pointer new_nodo(new t_node(_data));
if (n_elements == 0) {
it_begin = new_nodo;
it_end = it_begin;
} else {
it_end->next = new_nodo;
it_end = it_end->next;
}
n_elements++;
}
void push_front(const T& _data) {
cout << "Copy\n";
if (n_elements == 0)
push_back(_data);
else {
it_begin = shared_pointer (new t_node(_data, it_begin.pointer));
n_elements++;
}
}
//////Constructors
my_list(const std::initializer_list<T>& init_list) {
for (const T& elem : init_list)
this->push_back(elem);
}
};
我有一个问题。通过使用移动语义而不是const&reference传递数据时,可以提高构造函数,push_back和push_front的性能吗?