简体   繁体   中英

insert in C++ vector

I want to insert a bit from a bitset in the beginning of a vector. I am having a hard time understanding how to do that. Here is how I think I can do it:

keyRej.insert(x, inpSeq[0]);

I don't know what to put in the place of x?

I don't know what to put in the place of x?

An iterator to the position you want to insert in:

keyRej.insert(keyRej.begin(), inpSeq[0]);

Semantically, the inserted element goes before the iterator passed as first argument. But this will result in all elements of the vector having to be moved across one position, and may also incur a re-allocation of the vector's internal data storage block. It also means that all iterators or references to the vector's elements are invalidated.

See this reference for std::vector::insert for more information.

Note that there are containers, such as std::deque , for which appending elements to the front is cheap, and reference (but not iterator) validity is maintained.

根据您可能在此处阅读的文档,x是一个迭代器,新对象将插入在它之前。

keyRej.insert(keyRej.begin(), inpSeq[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM