push-back

C++ curious behavior in vector::push_back()

风流意气都作罢 提交于 2020-01-14 05:56:06
问题 I have the following data-structure as a class named "Task": private: string name; int computation_time; int period; Furthermore i have a ASCII-File with this content: A 3 10 B 2 12 C 1 11 name = A, computation_time = 3, period = 10 and so on.... Now i want to read in the file, create Task-object and push it back into a vector: void read_in_task_list_and_create_tasks(const string &filename, vector<Task> &current_tasks) { ifstream in_file; in_file.open(filename.c_str()); string tmp_name; int

Is there a back_inserter variant that takes advantage of move?

爷,独闯天下 提交于 2020-01-13 11:43:08
问题 In generic code I was trying to tell an output iterator (in practice a std::back_inserter_iterator to move a range of elements. To my surprise it looked as if elements were moved in a move-to-back_inserter operation. #include<algorithm> // move #include<iterator> // back_inserter #include<vector> int main(){ std::vector<std::vector<double> > v(10, std::vector<double>(100)); std::vector<std::vector<double> > w; assert( not v[0].empty() and w.size() == 0 ); std::copy(v.begin(), v.end(), std:

Is there a back_inserter variant that takes advantage of move?

谁说我不能喝 提交于 2020-01-13 11:43:08
问题 In generic code I was trying to tell an output iterator (in practice a std::back_inserter_iterator to move a range of elements. To my surprise it looked as if elements were moved in a move-to-back_inserter operation. #include<algorithm> // move #include<iterator> // back_inserter #include<vector> int main(){ std::vector<std::vector<double> > v(10, std::vector<double>(100)); std::vector<std::vector<double> > w; assert( not v[0].empty() and w.size() == 0 ); std::copy(v.begin(), v.end(), std:

Erroring about using push_back function in visual studio 2019 for Azure Kinect

北城以北 提交于 2020-01-06 05:22:09
问题 For line body_result_json["joint_orientations"].push_back({ skeleton.joints[j].orientation.wxyz.w, skeleton.joints[j].orientation.wxyz.x, skeleton.joints[j].orientation.wxyz.y, skeleton.joints[j].orientation.wxyz.z }); I got the error as below: nlohmann::basic_json<std::map, std::vector, std::string, bool, int64_t, uint64_t, double, std::allocator, nlohmann::adl_serializer>" has no member "push_back" offline_processor 来源: https://stackoverflow.com/questions/58595826/erroring-about-using-push

Push_back a variable to vector

 ̄綄美尐妖づ 提交于 2020-01-05 08:08:30
问题 Just started learning STL and here is the first problem: vector<int> vec1; for(int i = 1; i <= 100; i++) { vec1.push_back(i); cout << vec1[i] << endl; } As you may see i want to push back variable i to vector vec1 but output is: 5832900 -319008141 0 etc... Process returned 0 (0x0) execution time : 0.210 s Press any key to continue. Thanks for anything. 回答1: Your pushing on the back, but printing out item[i], which is one past the end (i starts at one in your loop). vector<int> vec1; for(int i

Segfault with std::list usage

自闭症网瘾萝莉.ら 提交于 2019-12-25 00:24:44
问题 I'm Java user coming over to C++, and I am having a hard time understanding what is going wrong with this statement. My program has been segfaulting anywhere I put the push_back command. So I'm wondering what exactly is going on. class Process { public: int nice; int arrivalTime; int cpuBursts; list<int> burstList; Process() { burstList.push_back(10); // Segfaults here... } }; Here is the full code: #include<iostream> #include<stdlib.h> #include<fstream> #include<list> #include<string.h>

Using push_back() for STL List in C++ causes Access Violation, Crash

荒凉一梦 提交于 2019-12-24 09:21:25
问题 I'm creating a game using my own homemade gaming engine, but I'm running into trouble using lists. I have a structure in my program called BoardState. Each of these structures has a list of BoardState pointers called children. This is because I create a tree of BoardStates for the AI of my game. To aid in the creation of my tree, I have a function called MakeBoard. This function gets passed all the information it needs to create a new board, and then it should add the pointer to that new

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

孤街醉人 提交于 2019-12-21 17:06:19
问题 I know the standard specifies that it is for vectors, but what about strings? 回答1: 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 ---------------+-------------

Updating vector of class objects using push_back in various functions

╄→尐↘猪︶ㄣ 提交于 2019-12-20 02:06:31
问题 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

vector::push_back vs vector::operator[]

谁说我不能喝 提交于 2019-12-18 03:00:32
问题 Below in c++ program, include<iostream> #include<vector> using namespace std; int main() { vector<int> numbers; numbers.push_back(2); numbers.push_back(10); numbers.push_back(5); numbers.push_back(3); numbers.push_back(7); numbers[3] = 8; numbers[5] = 11; for(int i=0; i<numbers.size(); ++i) { cout<<" "<<numbers[i]; } } see it on ideone. here, numbers[3] is working but numbers[5] . It looks like, vector::operator[] doesn't increase the size of vector like vector::push_back. so, is this the