vector

C++ nested for loop with erasing elements

你离开我真会死。 提交于 2021-02-11 14:10:27
问题 I would like to check all Elements of an vector against each other. By checking a condition an element should be removed. One approach was to erase the elements by nested for loops for (int a = 0; a < rs.size(); a++) { Point A = rs[a]; for (int b = 1; b <= rs.size(); b++) { Point B = rs2[b]; float distance = sqrt(pow(B.x - A.x, 2) + pow(B.y - A.y, 2) * 1.0); if (distance < 10.0) { if (distance > 0) { rs.erase(rs.begin() + b); } } } } but this would effect the vector and his size in runtime. A

C++ Magic 8 Ball with Vectors

社会主义新天地 提交于 2021-02-11 14:02:28
问题 I'm trying to take a Magic 8 Ball program that was originally using arrays and change it to a program that uses vectors instead. The task that I was given was to take the code below and do a couple of things to it. use the push_back() function to initialize the vector modify the signature and prototype of the getAnswer() function modify the code in the body of the getAnswer() function remove any unneeded code, such as your constant for the number of answers #include <iostream> #include

Search vector of structs

China☆狼群 提交于 2021-02-11 14:01:08
问题 I'm trying to find a string within in a vector of structs. But I keep getting an error, the error message is way too long so Ill upload it externally. A part of it is "error: no match for 'operator==' (operand types are 'char' and 'const std::__cxx11::basic_string') { return *__it == _M_value; }" The code Im currently using is: struct constants { std::string name, lastname; float salary=0; }; void searchPerson(const std::vector<constants> &search) { using namespace std; vector<constants>name;

Convert 2D vector to 2d array

爷,独闯天下 提交于 2021-02-11 12:16:34
问题 I have some C++ code that uses a 2D vector and needs to call an external library that uses a pointer the array with rows and col as parameters. So what I need to do is convert my 2d vector into a format that I can shove into foo((float *)data, int rows, int cols) . So 2D Vector data ---code---> foo((float *), int row, int cols) Trying to convert the 2d vector to a 2d array wasn't working very well using Converting 2D vector to 2D array This example uses double pointers and I don't know how to

MATLAB: Computing euclidean distance in an efficient way?

删除回忆录丶 提交于 2021-02-11 12:13:24
问题 What I am currently doing is computing the euclidean distance between all elements in a vector (the elements are pixel locations in a 2D image) to see if the elements are close to each other. I create a reference vector that takes on the value of each index within the vector incrementally. The euclidean distance between the reference vector and all the elements in the pixel location vector is computed using the MATLAB function "pdist2" and the result is applied to some conditions; however,

Which is faster, sorting a vector, then putting it into an AVL tree, or inputting it directly?

邮差的信 提交于 2021-02-11 09:55:27
问题 So here's the situation: I have millions, possibly billions, of strings that I am trying to parse and put into a sorted structure, lets say I have 5,000,000 strings. I'm trying to write a fast program that can put all of these strings from an unsorted vector into an ordered data structure that can also search the structure fast, thus the reasoning for the AVL tree (which eventually I plan to use a hash table of a-z for even faster lookup, but that comes later). I get all of the strings into a

Error when trying to store list in data.table of length 1

和自甴很熟 提交于 2021-02-11 08:13:40
问题 When trying to store a vector in a data.table, this works only when the data.table has length of more than one. Please find below a simplified version of the problem library(data.table) Working fine dt <- data.table( a = c("a", "b"), l = list()) dt$l[[1]] <- c(1:3) Results in: a l 1: a 1,2,3 2: b Producing Error dt <- data.table( a = c("a"), l = list()) dt$l[[1]] <- c(1:3) Error in [<-.data.table(x, j = name, value = value) : Supplied 3 items to be assigned to 1 items of column 'l'. The RHS

C++ Sort based on other int array

独自空忆成欢 提交于 2021-02-11 06:01:53
问题 suppose i have two vector std::vector<int>vec_int = {4,3,2,1,5}; std::vector<Obj*>vec_obj = {obj1,obj2,obj3,obj4,obj5}; How do we sort vec_obj in regard of sorted vec_int position? So the goal may look like this: std::vector<int>vec_int = {1,2,3,4,5}; std::vector<Obj*>vec_obj = {obj4,obj3,obj2,obj1,obj5}; I've been trying create new vec_array: for (int i = 0; i < vec_int.size(); i++) { new_vec.push_back(vec_obj[vec_int[i]]); } But i think it's not the correct solution. How do we do this?

C++ Sort based on other int array

╄→尐↘猪︶ㄣ 提交于 2021-02-11 06:01:36
问题 suppose i have two vector std::vector<int>vec_int = {4,3,2,1,5}; std::vector<Obj*>vec_obj = {obj1,obj2,obj3,obj4,obj5}; How do we sort vec_obj in regard of sorted vec_int position? So the goal may look like this: std::vector<int>vec_int = {1,2,3,4,5}; std::vector<Obj*>vec_obj = {obj4,obj3,obj2,obj1,obj5}; I've been trying create new vec_array: for (int i = 0; i < vec_int.size(); i++) { new_vec.push_back(vec_obj[vec_int[i]]); } But i think it's not the correct solution. How do we do this?

C++ Sort based on other int array

谁都会走 提交于 2021-02-11 06:01:25
问题 suppose i have two vector std::vector<int>vec_int = {4,3,2,1,5}; std::vector<Obj*>vec_obj = {obj1,obj2,obj3,obj4,obj5}; How do we sort vec_obj in regard of sorted vec_int position? So the goal may look like this: std::vector<int>vec_int = {1,2,3,4,5}; std::vector<Obj*>vec_obj = {obj4,obj3,obj2,obj1,obj5}; I've been trying create new vec_array: for (int i = 0; i < vec_int.size(); i++) { new_vec.push_back(vec_obj[vec_int[i]]); } But i think it's not the correct solution. How do we do this?