stdvector

How to use a std::vector in a C function

我的未来我决定 提交于 2019-12-01 08:27:30
A C function expects an array of buffers to be in scope at runtime. e.g. char values[x][y] The C function will populate the buffers I would like to use a dynamic array so I don't have to hard code the dimensions How do I use a std::vector in this situation? Just to be clear, I am using C++. The C function is contained in a library that I cannot modify. If you just want to pass the dynamic array encapsulated in a std::vector to a c routine you can pass a pointer to the head of the underlying array as: std::vector<char> myvector; // size-up myvector as needed foo(&myvector[0]); // pass a pointer

MPI send struct with a vector property in C++

旧街凉风 提交于 2019-12-01 06:42:52
I want to send a struct that has a vector property. typedef struct { int id; vector<int> neighbors; } Node; I know i have to create an MPI derived datatype as in this answer , but i don't know how to do it in my case, where i have a vector in the struct. If you want to stay high-level and send around objects, then Boost.MPI is a good choice. With Boost.MPI you specify high level serialization for your structs. You cannot (correctly) statically determine the offset of the data member of a vector. It is certainly possible to piece together a type, that works. But that is also a great way to

C++ Warming std vector

房东的猫 提交于 2019-12-01 06:20:26
Why filling std::vector second time is FASTER? Even if space was reserved from the beggining? int total = 1000000; struct BaseClass { float m[16]; int id; BaseClass(int _id) { id = _id; } }; int main() { std::vector<BaseClass> ar; ar.reserve(total); { auto t_start = std::chrono::high_resolution_clock::now(); for (int var = 0; var < total; ++var) { ar.emplace_back(var); } auto t_end = std::chrono::high_resolution_clock::now(); std::cout << std::chrono::duration_cast<std::chrono::milliseconds>( t_end - t_start).count() << "\n"; ar.clear(); } { auto t_start = std::chrono::high_resolution_clock:

How to convert vector<unsigned char> to int?

我只是一个虾纸丫 提交于 2019-12-01 06:19:41
I have vector<unsigned char> filed with binary data. I need to take, lets say, 2 items from vector(2 bytes) and convert it to integer. How this could be done not in C style? Please use the shift operator / bit-wise operations. int t = (v[0] << 8) | v[1]; All the solutions proposed here that are based on casting/unions are AFAIK undefined behavior, and may fail on compilers that take advantage of strict aliasing (e.g. GCC). You may do: vector<unsigned char> somevector; // Suppose it is initialized and big enough to hold a uint16_t int i = *reinterpret_cast<const uint16_t*>(&somevector[0]); //

vector<char> VS vector<bool> in C++11 [closed]

被刻印的时光 ゝ 提交于 2019-12-01 05:58:01
Why should we use vector<char> instead of vector<bool> ? What is the reason that vector<char> is faster? std::vector<bool> is a specialisation of std::vector<T> that's done mainly for space efficiency (debatable). However, it behaves similarly but not equally as a regular std::vector<T> . This is attributed mainly to the fact that std::vector<bool> is not a container in the usual C++ standard library sense but rather an array of bits. Some of the differences between a std::vector<bool> and a regular std::vector are: std::vector<bool>::iterator is not a random-access iterator. std::vector<bool>

How to convert vector<unsigned char> to int?

只愿长相守 提交于 2019-12-01 05:43:47
问题 I have vector<unsigned char> filed with binary data. I need to take, lets say, 2 items from vector(2 bytes) and convert it to integer. How this could be done not in C style? 回答1: Please use the shift operator / bit-wise operations. int t = (v[0] << 8) | v[1]; All the solutions proposed here that are based on casting/unions are AFAIK undefined behavior, and may fail on compilers that take advantage of strict aliasing (e.g. GCC). 回答2: You may do: vector<unsigned char> somevector; // Suppose it

How to use a std::vector in a C function

允我心安 提交于 2019-12-01 04:44:24
问题 A C function expects an array of buffers to be in scope at runtime. e.g. char values[x][y] The C function will populate the buffers I would like to use a dynamic array so I don't have to hard code the dimensions How do I use a std::vector in this situation? Just to be clear, I am using C++. The C function is contained in a library that I cannot modify. 回答1: If you just want to pass the dynamic array encapsulated in a std::vector to a c routine you can pass a pointer to the head of the

MPI send struct with a vector property in C++

China☆狼群 提交于 2019-12-01 04:32:13
问题 I want to send a struct that has a vector property. typedef struct { int id; vector<int> neighbors; } Node; I know i have to create an MPI derived datatype as in this answer, but i don't know how to do it in my case, where i have a vector in the struct. 回答1: If you want to stay high-level and send around objects, then Boost.MPI is a good choice. With Boost.MPI you specify high level serialization for your structs. You cannot (correctly) statically determine the offset of the data member of a

C++ Warming std vector

三世轮回 提交于 2019-12-01 04:01:32
问题 Why filling std::vector second time is FASTER? Even if space was reserved from the beggining? int total = 1000000; struct BaseClass { float m[16]; int id; BaseClass(int _id) { id = _id; } }; int main() { std::vector<BaseClass> ar; ar.reserve(total); { auto t_start = std::chrono::high_resolution_clock::now(); for (int var = 0; var < total; ++var) { ar.emplace_back(var); } auto t_end = std::chrono::high_resolution_clock::now(); std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(

Returning an empty vector of strings if key is not found

断了今生、忘了曾经 提交于 2019-12-01 03:43:53
问题 I know it is a very bad idea, so other suggestions on how to do it efficiently will be well-received. Here's the thing. I have map<string,vector<string> > , I want to search for a key and return its corresponding value (vector of strings in this case). Reason I insist on returning (rather than just iterating) is I need to search the values returned in some other vector. An example will make this clear: Input: key1 ---> {2,3,4} key2 ---> {1} key3 ---> {2,12,11,9} For key1 as input, vector with