ChatGPT解决这个技术问题 Extra ChatGPT

Using C++ vector::insert() to add to end of vector

I'm writing a little piece of code where I'll have to insert values into a C++ STL vector at a certain place depending on values in the vector elements. I'm using the insert() function to accomplish this. I realize that when I want to add a new element to the end of the vector, I could simply use push_back(). But to keep my code looking nice, I'd like to exclusively use insert(), which takes as input the iterator pointing to the element after the desired insertion point and the value to be inserted. If the value of the iterator passed in as an argument is v.end(), where v is my vector, will this work the same as push_back()?

Thanks a lot!

If you find yourself using insert a lot in a vector, you may be using the wrong data structure. Consider (for example) using a deque. Of course if the vector is small, there will be no problem.
@Nick: Yes it will. A simple experiment could have told you that.
@Space I don't see how an experiment could have told him that. If it were invalid, he'd get UB, in which case his program might well have appeared to work.
@unapersson: A deque would only help if the majority of the inserts is at the beginning or the end of the sequence. If it is only at the end of the sequence, a vector will do just as good.
@unapersson A deque is better for inserts at the beginning. It's almost certainly worse (both linear, but deque will have a significantly larger constant factor) for inserts anywhere but the beginning or the end. And the cost of inserting into the middle of a vector has often been overstated, at least if the types in the vector are POD types. (The real "cost" of insertion is often that it invalidates iterators. Only std::list avoids this, but std::list is extremely expensive otherwise.)

D
Daniel

a.push_back(x) is defined to have identical semantics to (void)a.insert(a.end(),x) for sequence containers that support it.

See table 68 in ISO/IEC 14882:2003 23.1.1/12 [lib.sequence.reqmts].

https://i.stack.imgur.com/fHYx0.png

Regarding the running time of vector.push_back(x) vs. vector.insert(vector.end(), x) consider the emphasized part:

Table 68 lists sequence operations that are provided for some types of sequential containers but not others. An implementation shall provide these operations for all container types shown in the ‘‘container’’ column, and shall implement them so as to take amortized constant time.


Quick question, in terms of performance , I wonder if pusk_back works faster? I tested something showing insert is kind of slow . I just wanted to make sure... Thanks
push_back() doesn't gives back the iterator of the newly inserted element. std::list::end() will give back a dead iterator.
Answer would be better if you included the section of the standard you reference
@JonMcClung: Um, but the answer already includes that information?
I meant as a quote. I don't know how to look up a random section of the standard from the chapter marker or whatever you gave.
n
neuront

There is a slight difference that push_back returns void whether insert returns iterator to element just inserted.

By the way, there is another way to verify whether they do the same thing: compile the following codes

int main()
{
    std::vector<int const> v;
    v.push_back(0);
    return 0;
}

the compiler will print a lot of annoying messages, just read and you will find push_back calls insert (if not, try compiling v.insert(v.end(), 0) to see if they call the same inserting function) in the end.