TinySTL icon indicating copy to clipboard operation
TinySTL copied to clipboard

Vector中的insert函数存在问题

Open jiangtao92 opened this issue 9 years ago • 2 comments

你好 我看过你的insert函数,存在一点问题,下面是我的测试代码

        namespace JJ
        {
#include <iostream>
            using namespace std;
            class TestItem
            {
            public:
                TestItem()
                {
                    ++count;
                    cout << "Construct" << endl;
                }

                TestItem(const TestItem & other)
                {
                    ++count;
                    cout << "Copy Construct" << endl;
                }

                virtual ~TestItem()
                {
                    --count;
                    cout << "Destructor" << endl;
                }

                static int getCount()
                {
                    return count;
                }
            private:
                static int count;
            };

            int TestItem::count = 0;

            void testCase16()
            {
                assert(TestItem::getCount() == 0);
                {
                    typedef TinySTL::vector<TestItem> TVector;
                    //typedef std::vector<TestItem> TVector;
                    TVector t(10);
                    t.push_back(TestItem());
                    t.push_back(TestItem());
                    t.push_back(TestItem());
                    //t.insert(t.begin(), 10, TestItem());
                    cout << t.capacity() << endl;

                    t.insert(t.begin(), t.begin(), t.begin() + 1);
                    cout << t.size() << endl;
                    t.clear();
                }
                assert(TestItem::getCount() == 0);

            }
        }

你的这个函数的实现里面,调用了

void insert_aux(iterator position, InputIterator first, InputIterator last, std::false_type);

jiangtao92 avatar Nov 24 '15 14:11 jiangtao92

问题在哪

zouxiaohang avatar Nov 24 '15 15:11 zouxiaohang

assert(TestItem::getCount() == 0); 不满足了

for (; tempPtr - position >= 0; --tempPtr){//move the [position, finish_) back
construct(tempPtr + locationNeed, *tempPtr);
}
...

这段代码应该是后移插入点之后的元素,但是应该分成两种情况来对待

  1. 一部分元素被移动到了finish_之后的内存上,需要调用construct
  2. 还有一部分元素可能被移动的先前的位置上,只需要调用operator ==
TinySTL::uninitialized_copy(first, last, position);

这段代码应该是用来插入新增的元素,同样需要考虑上面说到的两种情况

我觉得应该改成这样比较好点

        if (locationLeft >= locationNeed) {
            if (finish_ - position > locationNeed) {
                TinySTL::uninitialized_copy(finish_ - locationNeed, finish_, finish_);
                std::copy_backward(position, finish_ - locationNeed, finish_);
                std::copy(first, last, position);
            }
            else{
                iterator temp = TinySTL::uninitialized_copy(first + (finish_ - position), last, finish_);
                TinySTL::uninitialized_copy(position, finish_, temp);
                std::copy(first, first + (finish_ - position), position);
            }

jiangtao92 avatar Nov 24 '15 15:11 jiangtao92