vector

Efficiently moving contents of std::unordered_set to std::vector

久未见 提交于 2021-02-04 11:24:05
问题 In my code I have a std::unordered_set and I need to move the data into a std::vector . I'm using the std::unordered_set while getting the data to ensure only unique values are stored prior to converting to a std::vector . My question is how do I move the contents to the std::vector the most efficiently? I don't need the std::unordered_set after the data is moved. I currently have the following: std::copy(set.begin(), set.end(), std::back_inserter(vector)); 回答1: Before C++17, the best you can

R - Filter a vector using a function

对着背影说爱祢 提交于 2021-02-04 09:55:11
问题 I have a function similar to this one: isGoodNumber <- function(X) { if (X==5) return(TRUE) else return(FALSE) } I have a vector: v<-c(1,2,3,4,5,5,5,5) I want to obtain a new vector that contains the elements of v where isGoodNumber(v) == TRUE How do I do this ? Tried v [ isGoodNumber(v) == TRUE ] but it doesn't work :-) Thanks !! 回答1: You'll need to Vectorize the function to call it on a vector: isGoodNumber = Vectorize(isGoodNumber) v[isGoodNumber(v)] 回答2: There is a function named "Filter"

R ~ Vectorization of a user defined function

混江龙づ霸主 提交于 2021-02-02 09:23:45
问题 I need to write a function that will count the number of working days (minus weekends, and a vector of other local bank holidays), but the problem I'm coming up against is more simply illustrated with just counting the number of weekdays. Here is a function that will give the number of weekdays between two dates: removeWeekends <- function(end, start){ range <- as.Date(start:end, "1970-01-01") range<- range[sapply(range, function(x){ if(!chron::is.weekend(x)){ return(TRUE) }else{ return(FALSE

What is the size of sizeof(vector)? C++

我与影子孤独终老i 提交于 2021-02-02 03:46:07
问题 So the user inputs values within the for loop and the vector pushes it back, creating its own index. The problem arises in the second for loop, I think it has to do something with sizeof(v)/sizeof(vector) . vector<int> v; for (int i; cin >> i;) { v.push_back(i); cout << v.size() << endl; } for (int i =0; i < sizeof(v)/sizeof(vector); i++) { cout << v[i] << endl; } How will I determine the size of the vector after entering values? (I'm quite new to C++ so If I have made a stupid mistake, I

Cleaner way to convert an array of compact float values into an array of floats

余生颓废 提交于 2021-01-29 21:12:28
问题 Before the actual question, small prelude. I don't care about security, I do care about performance. I KNOW this is not proper and I know it's very hacky, however this is quite fast. vector<float> result = move(*((vector<float>*)&vertices)); That code is abusing C style casts and pointers to force the compiler to interpret the left hand side array vertices which is a vector of a compact type where all the fields are float as an array of floats. i.e struct vertex { float x; float y; float z; }

fortran matrix vector multiplication optimization

為{幸葍}努か 提交于 2021-01-29 16:30:58
问题 I tried to measure the difference of different matrix-vector-multiplication schemes in Fortran. I have actually written the following code: http://pastebin.com/dmKXdnX6 The 'optimized version' is meant to respect the memory layout of the matrix, by swapping the loops to access the matrix-elements. The provided code should compile with gfortran and it runs with the following rather surprising results: Vectors match! Calculations are OK. Optimized time: 0.34133333333333332 Naive time: 1

Overload operator + for vector: namespace std

ぐ巨炮叔叔 提交于 2021-01-29 15:57:01
问题 I am trying to overload the operator + and += for std::vector, and what I do is namespace std { template<class T> vector<T> operator+(vector<T> x, vector<T> y) { vector<T> result; result.reserve(x.size()); for (size_t i = 0; i < x.size(); i++) result[i] = x[i] + y[i]; return result; } } But I assume this is bad practice, because clang-tidy warns me "Modification of std namespace can result in undefined behavior". Is there other better practice in overloading operator for STL classes? 回答1:

Declaring 3D array structure in c++ using vector

北城以北 提交于 2021-01-29 07:37:29
问题 Hi I am a graduate student studying scientific computing using c++. Some of our research focus on speed of an algorithm, therefore it is important to construct array structure that is fast enough. I've seen two ways of constructing 3D Arrays. First one is to use vector liblary. vector<vector<vector<double>>> a (isize,vector<double>(jsize,vector<double>(ksize,0))) This gives 3D array structure of size isize x jsize x ksize. The other one is to construct a structure containing 1d array of size

Declaring 3D array structure in c++ using vector

怎甘沉沦 提交于 2021-01-29 07:32:29
问题 Hi I am a graduate student studying scientific computing using c++. Some of our research focus on speed of an algorithm, therefore it is important to construct array structure that is fast enough. I've seen two ways of constructing 3D Arrays. First one is to use vector liblary. vector<vector<vector<double>>> a (isize,vector<double>(jsize,vector<double>(ksize,0))) This gives 3D array structure of size isize x jsize x ksize. The other one is to construct a structure containing 1d array of size

Using custom class with vectors: 'std::vector' default constructor error

…衆ロ難τιáo~ 提交于 2021-01-29 05:52:03
问题 EDIT: Adding in a default constructor changed nothing, but adding in a : itemlist(0) initialiser to the Inventory constructor removed that particular error. However, multiple instances of these two error still occur: 'Item': undeclared identifier and 'std::vector': 'Item' is not a valid template type argument for parameter '_Ty' I'm wondering if there's some sort of scope issue happening here as regards to my two separate classes? I'm trying to create one class which defines an Item and