push-back

In C++, will the vector function push_back increase the size of an empty array?

谁说我不能喝 提交于 2019-12-05 09:04:36
Quick question. Let's say I declare a vector of size 20. And then I want to add a few integers to it using push_back. vector<int> myVector(20); myVector.push_back(5); myVector.push_back(14); Is the capacity of my vector now 22, or is it still 20? Were 5 and 14 added to indices [19] and [20], respectively? Or are they at [0] and [1]? After those statements its capacity is implementation-defined. (Please note that is different from its size.) vector<int> myVector(20); This creates a vector filled with twenty 0's. Its size is twenty, exactly, and its capacity is at least twenty. Whether or not it

Being Smart About Vector Memory Allocation

帅比萌擦擦* 提交于 2019-12-04 10:15:22
Let's say I have to iterate over a potentially very large vector of numbers and copy the even and odd elements into new, separate vectors. (The source vector may have any proportion of evens to odds; it could be all evens, all odds, or somewhere in-between.) For simplicity, push_back is often used for this sort of thing: for (std::size_t Index; Index < Source.size(); Index++) { if (Source[Index] % 2) Odds.push_back(Source[Index]); else Evens.push_back(Source[Index]); } However, I'm worried that this will be inefficient and be harmful if it's used as part of the implementation for something

In C++, is the amortized complexity of std::string::push_back() O(1)?

痞子三分冷 提交于 2019-12-04 08:40:22
I know the standard specifies that it is for vectors, but what about strings? Yes, it is amortized constant time. See table 101 on page 716 of this document : Table 101 lists operations that are provided for some types of sequence 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. Operation | Description | Container ---------------+----------------------+---------------------------------- a.push_back(t) | Appends a copy of t. | basic_string, deque,

C++ std::string append vs push_back()

别等时光非礼了梦想. 提交于 2019-12-03 08:32:29
问题 This really is a question just for my own interest I haven't been able to determine through the documentation. I see on http://www.cplusplus.com/reference/string/string/ that append has complexity: "Unspecified, but generally up to linear in the new string length." while push_back() has complexity: "Unspecified; Generally amortized constant, but up to linear in the new string length." As a toy example, suppose I wanted to append the characters "foo" to a string. Would myString.push_back('f');

Efficiency of C++11 push_back() with std::move versus emplace_back() for already constructed objects

蓝咒 提交于 2019-12-03 01:59:44
问题 In C++11 emplace_back() is generally preferred (in terms of efficiency) to push_back() as it allows in-place construction, but is this still the case when using push_back(std::move()) with an already-constructed object? For instance, is emplace_back() still preferred in cases like the following? std::string mystring("hello world"); std::vector<std::string> myvector; myvector.emplace_back(mystring); myvector.push_back(std::move(mystring)); // (of course assuming we don't care about using the

Vector of structs initialization

≯℡__Kan透↙ 提交于 2019-12-03 00:12:30
问题 I want know how I can add values to my vector of structs using the push_back method struct subject { string name; int marks; int credits; }; vector<subject> sub; So now how can I add elements to it? I have function that initializes string name(subject name to it) void setName(string s1, string s2, ...... string s6) { // how can i set name too sub[0].name= "english", sub[1].name = "math" etc sub[0].name = s1 // gives segmentation fault; so how do I use push_back method? sub.name.push_back(s1);

Vector of structs initialization

[亡魂溺海] 提交于 2019-12-02 13:55:12
I want know how I can add values to my vector of structs using the push_back method struct subject { string name; int marks; int credits; }; vector<subject> sub; So now how can I add elements to it? I have function that initializes string name(subject name to it) void setName(string s1, string s2, ...... string s6) { // how can i set name too sub[0].name= "english", sub[1].name = "math" etc sub[0].name = s1 // gives segmentation fault; so how do I use push_back method? sub.name.push_back(s1); sub.name.push_back(s2); sub.name.push_back(s3); sub.name.push_back(s4); sub.name.push_back(s6); }

Why vector hold a class type will call the copy constructor one more time when push_back()?

懵懂的女人 提交于 2019-12-02 06:09:29
问题 I have the following code: #include <iostream> using std::cin; using std::cout; using std::endl; #include <vector> using std::vector; class Quote { public: Quote() = default; Quote(const std::string &book, double sales_price): bookNo(book), price(sales_price) { } // Quote(const Quote&) = default; // memberwise copy Quote(const Quote &orig): bookNo(orig.bookNo), price(orig.price) { cout << orig.isbn() << endl; cout << "called Quote(const Quote &)" << endl; } Quote& operator=(const Quote&) =

Why vector hold a class type will call the copy constructor one more time when push_back()?

北战南征 提交于 2019-12-02 01:26:45
I have the following code: #include <iostream> using std::cin; using std::cout; using std::endl; #include <vector> using std::vector; class Quote { public: Quote() = default; Quote(const std::string &book, double sales_price): bookNo(book), price(sales_price) { } // Quote(const Quote&) = default; // memberwise copy Quote(const Quote &orig): bookNo(orig.bookNo), price(orig.price) { cout << orig.isbn() << endl; cout << "called Quote(const Quote &)" << endl; } Quote& operator=(const Quote&) = default; // copy assign std::string isbn() const { return bookNo; } virtual double net_price(std::size_t

Updating vector of class objects using push_back in various functions

元气小坏坏 提交于 2019-12-01 21:10:42
I have a vector of class objects I've created in main by reading in a data file. I'm then passing around the vector to several different files containing functions that perform different operations on the vector (sorting by different fields, subtracting inventory, etc.). I'm running into a problem when I try to use push_back to add to the vector in another file (that's a part of the same project) after it's already been created. The pre-existing vector is passed to the function and the vector is successfully added to within the function, but when I exit the function the added record is no