stdvector

Best way to erase vector of ranges from std::vector

◇◆丶佛笑我妖孽 提交于 2019-12-23 01:07:05
问题 In one of my projects it is necessary to remove certain elements from a std::vector<double> values . The indices I have to remove are given as a vector of intervals. For example {1,3} means, that I have to remove indices from 1 to 3 inclusive from values . I can assume that the intervals given are mutually exclusive. The code shown below illustrates, what the desired behavior should look like. #include <iostream> #include <vector> int main(int argc, char** args) { // Intervals of indices I

vector iterators incompatible

那年仲夏 提交于 2019-12-22 08:57:12
问题 I'm currently working on a graph library for C++ and now got stuck at a point where I get an assertion error in debug mode during runtime. I also had a look an some other question here on SO but none of the questions and answers lead me to a solution. After reading in some forums I have the impression that this error happens because iterators become invalid as soon as the vector content is changed. (for example when using erase() ) But as you can see in my code, I'm not modifying the vector,

Rename std::vector to another class for overloading?

爱⌒轻易说出口 提交于 2019-12-22 05:38:08
问题 Look at this code. #include <vector> template<class ...Args> using other_vector = std::vector<Args...>; template<class T> void f(std::vector<T>& ) {} template<class T> void f(other_vector<T>& ) {} int main() { other_vector<int> b; f(b); return 0; } It does not compile, because f is being redeclared. I totally understand the error. However, I need a second class that behaves like std::vector<T> , but will be seen as a different type, so that overloading, like in the above example, would be

Prettier syntax for “pointer to last element”, std::vector?

泄露秘密 提交于 2019-12-22 03:28:06
问题 I'm wondering if there is prettier syntax for this to get a normal pointer (not an iterator) to the last element in a C++ vector std::vector<int> vec; int* ptrToLastOne = &(*(vec.end() - 1)) ; // the other way I could see was int* ptrToLastOne2 = &vec[ vec.size()-1 ] ; But these are both not very nice looking! 回答1: int* ptrToLastOne = &vec.back(); // precondition: !vec.empty() 回答2: int* ptrToLast = &(vec.back()); // Assuming the vector is not empty. 回答3: Some more options: int* ptrToLast = &

C++: From stringstream to char**

。_饼干妹妹 提交于 2019-12-22 01:43:27
问题 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*>

VHDL STD_LOGIC_VECTOR Wildcard Values

ぐ巨炮叔叔 提交于 2019-12-21 12:32: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

How to “watch” the size of a C++ std::vector in gdb?

纵然是瞬间 提交于 2019-12-20 17:59:28
问题 I have a std::vector as part of a class, that contains a custom type. Its contents seems to be mysteriously changed from somewhere in the program. I am having trouble trying to figure out where this is happening. Is there a way to "watch" the contents (or size) of a std::vector from gdb? Thanks. 回答1: Is there a way to "watch" the contents (or size) of a std::vector from gdb? Assuming you are using GCC, set watchpoints on theVector->_M_impl._M_start and _M_finish . If you are using some other

Vector storage in C++

核能气质少年 提交于 2019-12-20 11:01:04
问题 I wish to store a large vector of d-dimensional points (d fixed and small: <10). If I define a Point as vector<int> , I think a vector<Point> would store in each position a pointer to a Point. But if define a Point as a fixed-size object like: std::tuple<int,int,...,int> or std::array<int, d> , will the program store all points in contiguous memory or will the additional level of indirection remain? In case the answer is that arrays avoid the additional indirection, could this have a large

Why does compiling over 100,000 lines of std::vector::push_back take a long time?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 09:23:51
问题 I'm compiling a C++ library which defines a single function that randomly samples from a set of data points. The data points are stored in a std::vector . There are 126,272 std::vector push_back statements, where the vector in question is of type double . It is taking a long time to compile. Why would this take so long? (All the code other than the std::vector push_back statements would take less than 1 second to compile, because there's very little other code.) 回答1: There is the -ftime

C++, copy set to vector

一个人想着一个人 提交于 2019-12-20 08:23:47
问题 I need to copy std::set to std::vector : std::set <double> input; input.insert(5); input.insert(6); std::vector <double> output; std::copy(input.begin(), input.end(), output.begin()); //Error: Vector iterator not dereferencable Where is the problem? 回答1: You need to use a back_inserter : std::copy(input.begin(), input.end(), std::back_inserter(output)); std::copy doesn't add elements to the container into which you are inserting: it can't; it only has an iterator into the container. Because