开发者

how to write "insert" in vector? [closed]

开发者 https://www.devze.com 2023-03-11 14:39 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot 开发者_如何学运维be reasonably answered in its current for
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot 开发者_如何学运维be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I write this,but it didn't work...

template<typename T>
typename vector<T>::iterator vector<T>::insert(iterator pos,const T& val)
{
    int a=0,b=0;
    if(capacity()==0) a=capacity()+1;
    if(capacity()==size())
    {
        a=capacity()*2;
        T *start2=operator new[](a*sizeof(T)))
        for(int i=0;;i++)
        {
             new(start2+i) T(start+i);
             if(start2+i==pos)
             {
                 b=start2+i;
                 break;
             }
        }
        start2+pos=val;
        for(int j=b;j<=2*size();j++)
        {
             new(start2+j) T(start+j+1);
        }
    }
    else if(capacity()>size())
    {
        for(int i=0;i<=capacity();i++)
        {
            if(start+i==pos)pos=
        }
    }     
}


Use insert() or push_back() functions provided for std::vector


There is already an insert() function in std::vector.

0

精彩评论

暂无评论...
验证码 换一张
取 消