stdvector

Using STL vector with SIMD intrinsic data type

China☆狼群 提交于 2020-01-13 03:14:13
问题 As the title reads, I am trying to use STL vector with SIMD intrinsic data type. I know it is not a good practice due to the potential overhead of load/store, but I encountered a quite weird fault. Here is the code: #include "immintrin.h" #include <vector> #include <stdio.h> #define VL 8 int main () { std::vector<__m256> vec_1(10); std::vector<__m256> vec_2(10); float * tmp_1 = new float[VL]; printf("vec_1[0]:\n"); _mm256_storeu_ps(tmp_1, vec_1[0]); // seems to go as expected for (int i = 0;

How to cheaply assign C-style array to std::vector?

*爱你&永不变心* 提交于 2020-01-09 05:04:26
问题 Currently I do the following: // float *c_array = new float[1024]; void Foo::foo(float *c_array, size_t c_array_size) { //std::vector<float> cpp_array; cpp_array.assign(c_array, c_array + c_array_size); delete [] c_array; } How can I optimize this assigning? I would like not to perform elementwise copy but just swap pointers. 回答1: The current std::vector doesn't provide any capability or interface to take ownership of previously allocated storage. Presumably it would be too easy to pass a

Vector of const objects giving compile error

核能气质少年 提交于 2020-01-08 21:45:09
问题 I have declared the following in my code vector <const A> mylist; I get the following compile error - new_allocator.h:75: error: `const _Tp* __gnu_cxx::new_allocator<_Tp>::address(const _Tp&) const \[with _Tp = const A]' and `_Tp* __gnu_cxx::new_allocator<_Tp>::address(_Tp&) const [with _Tp = const A]' cannot be overloaded But if declare - vector <A> mylist; my code compiles. Is const not allowed in this context? I am copying my code here for everyone'e reference - #include <iostream>

Debug Assertion : Vector iterators incompatible (C++)

不问归期 提交于 2020-01-07 04:48:09
问题 for (int i = 0; i < m_pGameField->dimensions()->first; i++) { for (int j = 0; j < m_pGameField->dimensions()->second; j++) { for (std::vector<std::shared_ptr<GameGridElement>>::iterator it = m_pGameField->getField(i, j)->getElements().begin();it != m_pGameField->getField(i, j)->getElements().end(); ++it) { if ((*it)->getName().compare("Spawn") == 0) { tmpSpawns.push_back(std::static_pointer_cast<SpawnElement>((*it))); } } } } Hey Guys, i have some problems with the upper code. The code

Create variable number of std::threads

旧城冷巷雨未停 提交于 2020-01-06 03:49:25
问题 I have a threaded program that I have to run on multiple computers. Each of them have a different number of supported threads. In the computer that I developed the program there are 4 threads , and so I hard coded 4 threads to be created. I want to make this vary according to the situation . I want to use std::thread::hardware_concurrency to get the number of threads , and divide the work into the number of threads available . Is this possible ? The hard coded thread creation is : //const

Is accessing an array is faster than accessing vector? [duplicate]

蓝咒 提交于 2020-01-06 01:54:15
问题 This question already has answers here : Closed 9 years ago . Possible Duplicates: Using arrays or std::vectors in C++, what's the performance gap? std::vector is so much slower than plain arrays? memory is vector of 1000 elements array[] is an integer array of 1000 elements for (iteration = 0; iteration < numiterations; iteration++) { for (j = 1; j < numints; j++) { memory[j] += memory[j - 1]; //array[j] += array[j - 1]; } } If I compare the time of the for loop after running 100 iterations,

Reading and writing a vector of classes

安稳与你 提交于 2020-01-05 08:43:50
问题 I have an inheritance hierarchy with three/four levels. And within each level will hold at least one or more classes with different attributes that make that object of a class unique (and of course, inheriting attributes from the level above). Each object of a class may have different attributes to another therefore my question is, how can I read and write each object to a file and differentiate the attributes? I do apologise if I have not worded this very well but will be very appreciative

Overload vector subscript operator to take a char * or string

烂漫一生 提交于 2020-01-05 05:28:12
问题 I am trying to overload the subscript operator -i know it as the element access operator- to take a char * or a std::string. I have a struct struct foo { int Age; const char *Name; }; and a std::vector that will be holding multiple instances of this struct. std::vector<foo>bar; my goal is to be able to access each foo in bar by calling them by their name. std::cout<<"Simon's age is: "<<bar["simon"]; I've just been searching google for a long while trying to find an example or something to go

Sending large std::vector using MPI_Send and MPI_Recv doesn't complete

╄→尐↘猪︶ㄣ 提交于 2020-01-05 04:17:07
问题 I'm trying to send a std::vector using MPI. This works fine when the the vector is small, but just doesn't work when the vector is large (more than ~15k doubles in the vector). When trying to send a vector with 20k doubles, the program just sits there with the CPU at 100%. Here is a minimal example #include <vector> #include <mpi.h> using namespace std; vector<double> send_and_receive(vector<double> &local_data, int n, int numprocs, int my_rank) { MPI_Send(&local_data[0], n, MPI_DOUBLE, 0, 0,

Storing and Restoring std::vector from NSData

风格不统一 提交于 2020-01-03 01:35:41
问题 I am attempting to store std::vector to NSData and back directly. My first attempt I converted each point to an NSValue and stored them with NSKeyedUnarchiver which seems terribly inefficient. My test dataset required 64MB of human readable text (with NSKeyedUnarchiver), versus converting each std:vector to NSData the resulting stored files is a much more reasonable 896kb. I am doing as follows to store the data: typedef std::vector<CGPoint> CGContour; typedef std::vector<std::vector<CGPoint>