Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector中的insert函数存在问题 #7

Open
jiangtao92 opened this issue Nov 24, 2015 · 2 comments
Open

Vector中的insert函数存在问题 #7

jiangtao92 opened this issue Nov 24, 2015 · 2 comments

Comments

@jiangtao92
Copy link
Contributor

你好
我看过你的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);
@zouxiaohang
Copy link
Owner

问题在哪

@jiangtao92
Copy link
Contributor Author

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);
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants