stdvector

Is inserting an element of a std::vector into the same vector allowed?

北城余情 提交于 2019-12-04 22:51:41
Consider the following insert and emplace member functions of std::vector<T> : template <class... Args> iterator emplace(const_iterator position, Args&&... args); iterator insert(const_iterator position, const T& x); iterator insert(const_iterator position, T&& x); iterator insert(const_iterator position, size_type n, const T& x); What if one of them is invoked with a reference to an element of the vector itself as an argument? Normally, each of them invalidates references to all elements starting from position , which might include the argument, or if a reallocation happens, references to all

How to convert std::queue to std::vector

梦想与她 提交于 2019-12-04 22:51:10
问题 I need to make use of a queue of doubles because of the good properties it has as an ordered container. I want to pass this queue to a class constructor that accepts vectors. If I do that directly I get the following error: candidate constructor not viable: no known conversion from 'std::queue' to 'std::vector &' for 2nd argument How to cast a queue to a vector? 回答1: The correct container to model both queue_like behaviour and vector-like behaviour is a std::deque . This has the advantages of

C++: From stringstream to char**

拟墨画扇 提交于 2019-12-04 21:32:28
I have a class with parse(int argc, char* argv[]) function which I have to use to set a desired state of an object. I'm taking the parameters from the gui using stringstream and then I'm trying to convert them to char** to pass them to the function. Here's what I've got: std::stringstream sstream; sstream << "-clip" << " " << min_x_entry.get_text() << " " << max_x_entry.get_text(); // etc. std::cout << sstream.str(); // All looks good here std::vector<std::string> args; std::vector<char*> argv; std::string arg; while (sstream >> arg) { args.push_back(arg); argv.push_back(const_cast<char*>(args

Getting the size in bytes of a vector [duplicate]

主宰稳场 提交于 2019-12-04 19:21:49
问题 This question already has answers here : sizeof() std::vector (C++) (2 answers) Closed 6 years ago . Sorry for this maybe simple and stupid question but I couldn't find it anywhere. I just don't know how to get the size in bytes of a std::vector. std::vector<int>MyVector; /* This will print 24 on my system*/ std::cout << "Size of my vector:\t" << sizeof(MyVector) << std::endl; for(int i = 0; i < 1000; i++) MyVector.push_back(i); /* This will still print 24...*/ std::cout << "Size of my vector

Convert vector<int> to integer

家住魔仙堡 提交于 2019-12-04 09:55:37
I was looking for pre-defined function for converting a vector of integers into a normal integer but i din't find one. vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); Need this: int i=123 //directly converted from vector to int Is there a possible way to achieve this? Using C++ 11: reverse(v.begin(), v.end()); int decimal = 1; int total = 0; for (auto& it : v) { total += it * decimal; decimal *= 10; } EDIT: Now it should be the right way. EDIT 2: See DAle's answer for a shorter/simpler one. For the sake of wrapping it into a function to make it re-usable. Thanks @Samer int

Copy from vector<pointer*> to vector<pointer*> in C++

偶尔善良 提交于 2019-12-04 09:53:49
I create a vector A and want to copy to a vector B in another class by using below method, is it a correct way? The vector A may be destroyed! I searched in google, but not found the good solution and meaningful explanation. Thanks everyone void StateInit(vector<CButton*> listBtn) { _m_pListBtn = listBtn; }; Yes and no, you are passing the vector by value: void StateInit(vector<CButton*> listBtn) { _m_pListBtn = listBtn; }; Wich means that listBtn is a copy of vector A (asuming we are calling vector A the one passed as parameter of StateInit), if you delete vector A, vector B will still have

VHDL STD_LOGIC_VECTOR Wildcard Values

蓝咒 提交于 2019-12-04 07:21:17
I've been trying to write a Finite State Machine in VHDL code for a simple 16-bit processor I'm implementing on an Altera DE1 board. In the Finite State Machine, I have a CASE statement that handles the different 16-bit instructions, which are brought into the FSM by a 16-bit STD_LOGIC_VECTOR. However, I'm having a little trouble in the decode state where the Finite State Machine decodes the instruction. One of the instructions is an ADD which takes two registers as operands and a third as the destination register. However, I also have an ADD instruction which takes a register and a 5-bit

Vector Initialisation in C++

六眼飞鱼酱① 提交于 2019-12-04 05:31:40
问题 I am using Vectors in my code. The line that is causing the error is as follows : vector<Node> alt_seq ; alt_seq = vector<Node>(1000); for(int j=0; j<alt_cf.getNoOfNodes(i); j++) { Node temp_node = *alt_itr; alt_itr++; alt_seq.push_back(temp_node); } The line : alt_seq.push_back(temp_node); causes a runtime error. However if I initialise the Vector with some initial size as follows: vector<Node> alt_seq(1000) ; In this case the code works fine. However I do not want to give an initial size as

why there is no find for vector in C++

一个人想着一个人 提交于 2019-12-04 04:14:20
what's the alternative? Should I write by myself? There is the std::find() algorithm, which performs a linear search over an iterator range, e.g., std::vector<int> v; // Finds the first element in the vector that has the value 42: // If there is no such value, it == v.end() std::vector<int>::const_iterator it = std::find(v.begin(), v.end(), 42); If your vector is sorted, you can use std::binary_search() to test whether a value is present in the vector, and std::equal_range() to get begin and end iterators to the range of elements in the vector that have that value. The reason there is no

C++: Automatic vector reallocation invokes copy constructors? Why?

只谈情不闲聊 提交于 2019-12-04 03:31:49
I'm reading C++ Primer, 3rd Ed (Lippman and Lajoie) and it's saying that when a vector needs to be reallocated in order to make space for more elements added with push_back() , the elements are copy-constructed in the new space and then the destructor is called on the old elements. I'm confused about why this is necessary - why can't the data just be copied bit-for-bit? I assume that the answer has to do with dynamic memory allocation, but my current line of reasoning is that even if the vector elements handle dynamic memory, the data actually stored in the elements will be pointers, meaning