fix error in insert

This commit is contained in:
2024-02-26 00:05:09 +00:00
parent 78abd9ef99
commit 8b1e8e6d34
2 changed files with 5 additions and 2 deletions

View File

@ -95,8 +95,11 @@ void TestInsert()
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
std::cerr<<"Now back is"<< v.back()<<std::endl;
v.insert(v.begin() + 3, 100);
std::cerr<<"Now back is"<< v.back()<<std::endl;
v.insert(v.begin() + 5, 200);
std::cerr<<"Now back is"<< v.back()<<std::endl;
for (sjtu::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {
std::cout << *it << " ";
}

View File

@ -368,7 +368,7 @@ class vector {
raw_end = raw_beg + current_length;
allocated_length = new_allocated_length;
}
for (T *i = raw_end - 1; i != pos.raw_pointer; --i) {
for (T *i = raw_end; i != pos.raw_pointer; --i) {
std::allocator_traits<decltype(alloc)>::construct(alloc, i, std::move(*(i - 1)));
std::allocator_traits<decltype(alloc)>::destroy(alloc, i - 1);
}
@ -398,7 +398,7 @@ class vector {
raw_end = raw_beg + current_length;
allocated_length = new_allocated_length;
}
for (T *i = raw_end - 1; i != raw_beg + ind; --i) {
for (T *i = raw_end; i != raw_beg + ind; --i) {
alloc.construct(i, std::move(*(i - 1)));
alloc.destroy(i - 1);
}