vector

Can you pop_back a vector and still use the iterator to the last element?

醉酒当歌 提交于 2021-02-18 21:29:26
问题 I wonder what happens if I have an iterator on the last element of the vector and do pop_back . std::set<int> s; s.insert(5); std::vector<int> v = {1, 2, 3, 4, 5}; for (auto it = v.begin(); it != v.end();) { if (s.count(*it)) { std::swap(*it, v.back()); v.pop_back(); } else { ++it; } } Code above works properly ( v is {1, 2, 3, 4} after that block) at least with clang, but is it correct to check if it == v.end() if it is invalidated? 回答1: Your instincts are good; vector::pop_back invalidates

plotting normal vector in 3d

≡放荡痞女 提交于 2021-02-18 17:05:51
问题 I have a normal vector that I calculated from the cross product of two vectors [xn,yn,zn] and I have a point[x0,y0,z0] how can I plot the normal in 3d. I didn't know how to do it. Any suggestions please? 回答1: With Arrow heads: a = [2 3 5]; % your point [x0,y0,z0] b = [1 1 0]; % your normal vector c = a+b; % end position of normal vector %quiver3 syntax: quiver3(x,y,z,u,v,w) quiver3(a(1), a(2), a(3), c(1), c(2), c(3)); axis equal; This will hopefully draw a vector from your point into the

plotting normal vector in 3d

此生再无相见时 提交于 2021-02-18 17:04:25
问题 I have a normal vector that I calculated from the cross product of two vectors [xn,yn,zn] and I have a point[x0,y0,z0] how can I plot the normal in 3d. I didn't know how to do it. Any suggestions please? 回答1: With Arrow heads: a = [2 3 5]; % your point [x0,y0,z0] b = [1 1 0]; % your normal vector c = a+b; % end position of normal vector %quiver3 syntax: quiver3(x,y,z,u,v,w) quiver3(a(1), a(2), a(3), c(1), c(2), c(3)); axis equal; This will hopefully draw a vector from your point into the

Pushing an object with unique_ptr into vector in C++

一曲冷凌霜 提交于 2021-02-18 10:40:15
问题 I have a simple class structure modelling a discrete simulation, with a vector of States, which each contain a number of Transitions, held as a vector of smart pointers. I've used smart pointers to hold the transitions as in my full application I need polymorphism. #include <vector> #include <memory> class Transition { public: Transition() {} }; class State { public: State(int num) : num(num), transitions() {} void add_transition(std::unique_ptr<Transition> trans) { transitions.push_back(std:

Pushing an object with unique_ptr into vector in C++

可紊 提交于 2021-02-18 10:40:09
问题 I have a simple class structure modelling a discrete simulation, with a vector of States, which each contain a number of Transitions, held as a vector of smart pointers. I've used smart pointers to hold the transitions as in my full application I need polymorphism. #include <vector> #include <memory> class Transition { public: Transition() {} }; class State { public: State(int num) : num(num), transitions() {} void add_transition(std::unique_ptr<Transition> trans) { transitions.push_back(std:

Efficient accumulate

故事扮演 提交于 2021-02-18 06:18:11
问题 Assume I have vector of strings and I want concatenate them via std::accumulate. If I use the following code: std::vector<std::string> foo{"foo","bar"}; string res=""; res=std::accumulate(foo.begin(),foo.end(),res, [](string &rs,string &arg){ return rs+arg; }); I can be pretty sure there will be temporary object construction. In this answer they say that the effect of std::accumulate is specified this way: Computes its result by initializing the accumulator acc with the initial value init and

Efficient accumulate

我的梦境 提交于 2021-02-18 06:18:07
问题 Assume I have vector of strings and I want concatenate them via std::accumulate. If I use the following code: std::vector<std::string> foo{"foo","bar"}; string res=""; res=std::accumulate(foo.begin(),foo.end(),res, [](string &rs,string &arg){ return rs+arg; }); I can be pretty sure there will be temporary object construction. In this answer they say that the effect of std::accumulate is specified this way: Computes its result by initializing the accumulator acc with the initial value init and

Java increase array by X size

浪子不回头ぞ 提交于 2021-02-17 05:18:20
问题 I need an array that I can determine its size from the start and if it has no more empty spaces then increase its size by X . For example: int arraySize = 2; int[] intArray = new int[arraySize]; int counter = 0; while(something){ if(counter == arraySize){ // increase array size by X } intArray[counter] = counter; counter++; } I checked ArrayList and Vector but I did not find a way. 回答1: I think ArrayList is better in this case instead of simple arrays because automatically grows. From the

Create separate vectors for each of a data frame's columns (variables)

馋奶兔 提交于 2021-02-16 20:05:25
问题 Goal: Take a data frame and create separate vectors for each of its columns (variables). The following code gets me close: batting <- read.csv("mlb_2014.csv", header = TRUE, sep = ",") hr <- batting[(batting$HR >= 20 & batting$PA >= 100), ] var_names <- colnames(hr) for(i in var_names) { path <- paste("hr$", i, sep = "") assign(i, as.vector(path)) } It creates the a vector for each column in the data frame as shown by the output below: > ls() [1] "AB" "Age" "BA" "batting" "BB" "CS" [7] "G"

Create separate vectors for each of a data frame's columns (variables)

假装没事ソ 提交于 2021-02-16 20:04:28
问题 Goal: Take a data frame and create separate vectors for each of its columns (variables). The following code gets me close: batting <- read.csv("mlb_2014.csv", header = TRUE, sep = ",") hr <- batting[(batting$HR >= 20 & batting$PA >= 100), ] var_names <- colnames(hr) for(i in var_names) { path <- paste("hr$", i, sep = "") assign(i, as.vector(path)) } It creates the a vector for each column in the data frame as shown by the output below: > ls() [1] "AB" "Age" "BA" "batting" "BB" "CS" [7] "G"