C++ :: Store Coordinates As X And Y In A Vector To Find Nearest Neighbor Of Given Points
Oct 13, 2014
Lets say that i have the coordinates of a 2D space (x and y), I want to store the coordinates as x and y in a vector to find the nearest neighbor of given points like i have:
Each coordinates is like x y and it shows points 1 to 10 (like 3.57 3.18 is point 1 in 2D space ). My question is that how i add all x and y coordinates in vector? I started like this;
In this project you are asked to find K nearest neighbors of all points on a 2D space. The distance metric that you are going to use is simply the Euclidean distance example;
Currently I am working on a program that will find the actual distance between two points on a grid. It also determines the angle of the line segment in degrees. Now, i need it to be able to find any other points on or near the line. It will be running in a loop to find each additional point sequentially until all the points have been plotted. Unfortunately, I am not entirely sure how this is done. So far, I think that I could develop an algorithm that converts the angle into a ratio of vertical movements to horizontal ones.
I'm making a system like twitter for class called ShoutOut.com I want to be able to get the PublicShoutOut pointer pointed to by the start iterator and assign it to firstShoutOutToDisplay and secondShoutOutToDisplay because I need that in order to pass the pointers to one of my functions. When I step through the debugger the values in start are all default values like "" and so are the values in this->firstShoutOutToDisplay but the message that start points to is being output just fine.
EDIT: got rid of irrelevant code. Am I using the correct syntax to do this?
if (start != finish) { //getting these because a shoutout needs to be passed to the function that displays //options for a shoutout this->firstShoutoutToDisplay = (*start);
I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.
For instance, here is a test program that successfully finds the maximum according to just one member of the structure:
And the output was this, as expected: Maximum element S.a of vector<S> vec is at: 9 [I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6 [I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7 [I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8 [I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9
However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.
This was the description of the std::find function that I found: find - C++ Reference
Code: template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);
I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference
Code: template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:
Code: int x=7; std::vector<S>::iterator result; result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;
Is there another std function that I can use to make search and find an element according to just one member of myStruct?
Or perhaps there is a clever way to pass two arguments to the unary predicate.
I am having trouble with parsing out string value into a 2D vector. Suppose i have the string "attack at dawn " consisting of 15 characters, i will like to store it into a 2D vector with 5 rows and 3 columns and the result is as follow.
Vector[0][0] = "a" Vector[0][1] = "t" Vector[0][2] = "t" Vector[1][0] = "a" Vector[1][1] = "c"
I am currently trying to implement a 2d vector to store x and y of type int.
I have successfully passed it to the function, however i am unable to store the values in it. It always returns with a segmentation fault and my program terminates from there. May i know how do i store them properly and call them out?
Below is my code snippet
int reconstructSecret(int x, int y, vector< vector<int> > &inVec ,int constructSecret) { int getX,getY,formula,accum,count,getSecret,startPosition,nextPosition,numerator,denominator; getX=x; getY=y; int result;
[Code] .....
The main method
vector< vector<int> > inVec; for(int i=0;i<constructSecret;i++) { cout<<"please key in the "<<i<< "share of x value:"; cin>>x;
I am having trouble with parsing out string value into a 2D vector. Suppose I have the string "attack at dawn " consisting of 15 characters, i will like to store it into a 2D vector with 5 rows and 3 columns and the result is as follow.
Vector[0][0] = "a" Vector[0][1] = "t" Vector[0][2] = "t" Vector[1][0] = "a" Vector[1][1] = "c" Vector[1][2] = "k" Vector[2][0] = " " Vector[2][1] = "a" Vector[2][2] = "t" etc...
Here is a draft code that i did but is not working as desired.
vector<vector <string > > plaintextVector; vector<string> row; string totalString = "attack at dawn "; int dimension = 3;
Overview of problem : I am using std::vector to hold objects of Subject. Now this vector contains lots of objects( with lots I mean 10-20 objects at max) . These objects have string member values like category and sub_category. Both category and sub_category can have string which can be same of other objects's sub_category & category.
Issue: Now I want my std::vector to have only those objects whose's sub_category are unique. If category is not unique that's not a problem .
Secondly if we found 2 objects having same sub_category then we have to delete one of them from the vector. we will delete it based on some rules example
Rules for deleting are if
i) instance of Subject ->category = " Land " OR if category = "Jungle" then delete other duplicate object , ii) if above condition doesn't match then delete either of them.
I am wondering , how would I compare the sub-items from the vector . For example. I have class say Subject
class Subject { public : // some constructors, // functions to get ., set category and sub category std::String get_sub_category() std::string get_category(); private: std::string category; std::string sub_category; }
I have vector which stores object of Subjects. Example : vector<Subject> copy_vector;
Now what I want is to delete the object from vector that has same sub_category I am not looking for source code buT i need a starting point,? Example:
copy_vector[0] = Animal object that has sub_category Tiger copy_vector [1] = Animal object with Lion as sub category copy_vector[2] = Forest object with sub_category Tiger
What I want is to based on some conditions(which I can do ) remove either Forest or Animal object containing Tiger. But for that how would I do comparison? I have written the function and have checked it.
std::vector< Subject >copy_vector; // copy_vector contains all the objects of Subject with redundant sub_category for( std::vector< Subject >::iterator ii = copy_vector.begin() ; ii != copy_vector.end() ; ++ii ) { sub_category = ii->get_sub_category();
You are to write a C++ program to generate random integers in the range [ LOW = 1, HIGH = 10000 ] and to store them in a vector < int > of size VEC_SIZE = 250. Then, sort the contents of the vector (in ascending order) and display it on stdout.
To sort the contents of a vector, use the sort ( ) function from the STL. In addition to the main ( ) routine, implement the following subroutines in your program:
• void genRndNums ( vector < int >& v ) : This routine generates VEC_SIZE integers and puts them in vector v. Initializes the random number generator (RNG) by calling the function srand ( ) with the seed value SEED = 1, and generates random integers by calling the function rand ( ).
• void printVec ( const vector < int >& v ) : This routine displays the contents of vector v on stdout, printing exactly NO_ITEMS = 12 numbers on a single line, except perhaps the last line. The sorted numbers need to be properly aligned on the output. For each printed number, allocate ITEM_W = 5 spaces on stdout.
Programming Notes:
• You are not allowed to use any I/O functions from the C library, such as scanf or printf. Instead, use the I/O functions from the C++ library, such as cin or cout. • Let v be a vector of integers, then the call: sort ( v.begin ( ), v.end ( ) ) sorts the elements of v in ascending order. The detailed description of the sort ( ) routine can be found on the course web site and in the course textbook. • Execute the srand ( ) function only once before generating the first random integer with the given seed value SEED. The rand ( ) function generates a random integer in the range [ 0, RAND_MAX ], where the constant value RAND_MAX is the largest random integer returned by the rand ( ) function and its value is system dependent. To normalize the return value to a value in the range [ LOW, HIGH ], execute: rand ( ) % ( HIGH – LOW + 1 ) + LOW.
I'm having trouble with rounding to the nearest cent in this program. It's the only thing that i need. The result i need is just off by one cent. I tried multiplying the interest amount by 100 and adding .5 then dividing all that by 100 but that just made it worse. Here's the code:
Code: #include <stdio.h> int main() { int count=0;
Example; 0 to 0.2 = 0 0.3 to 0.5 = 0.5 0.6-0.9 = 1
What's the formula for C++?
I found a way to do it in math but I want to know what I'd have to put in C++. It's (x*2+.5)remove decimal numbers and divide by 2. What do I put in place of "remove decimal"?
For ex; x = 8.4 using (x*2+.5)remove decimal then divide by 2. 8.4 * 2 = 16.8 16.8 + .5 = 17.3 17.3 - decimal = 17 17/2 = 8.5
Rounding a numerical figure up to the nearest hundred. E.G.:
256 >> 300 654 >> 700 15 >> 100
I would like to know the formula to enter into Visual Basic 2008. I'm making a calculator Program, and i need a function that rounds up to the nearest 100...
So the program reads contents from a text file into a vector and then the user enters in a string that they want to search for. The program iterates through the vector to find the string and then saves that line to another vector to display later(incase there is more then 1 instance of the string found).
Here is what I have atm:
void doSearch(vector<string> &phonelist, string searcher, vector<string> &holdNumbers) { int i = 0; string value;
[Code].....
I just get an R6010 error -abort() has been called.
Ok my assignment has me doing vector math with some canned code provided for me by the instructor This is the header file to the class I'm working with and the .cpp file as far as I've gotten it.
#pragma once #include "Scalar.h" class Vector2D { public:
Vector2D(); Vector2D( const Vector2D& ) ;// copy constructor Vector2D( Scalar element[2] ) ; // initialize with an array
[Code] ....
I'm having trouble seeing which data members I'm multiplying together and what the initial state, continuing state, and after loop action I'm supposed to be using in the for loop.
I defined the following function to find out the iterator of a certain value in the vector. I defined it as such so if the value exist in the vector then return a iterator of it, if not then return a pointer pointing to nonsense.:
// objects to hold results, row id, and name class result_holder { public: // initialize class members result_holder() : row_id(0), row_value(0.0), row_name("") { }
[Code] ....
There are cases where I need to find an object based on the value of row_id and delete the object from the vector row_results. I could find the proper object by looping through the vector and testing against each member.
Code: // id I am looking for unsigned int id_to_delete = 12; for(i=0; i<row_results.size(); i++) { if(id_to_delete == row_results[i].row_id) { delete row_results[i]; } }
I have used find before to find the position in a vector with a specific value, but I don't know how to use find to locate a specific value for an object member.
Also, is delete what I need to get rid of the object or should I be using erase as in,
Code: // id I am looking for unsigned int id_to_delete = 12; for(i=0; i<row_results.size(); i++) { if(id_to_delete == row_results[i].row_id) { row_results.erase(row_results.begin()+i); } }
Okay, so for an assignment I need to write a function called find() that returns a reference to a vector. So I have vector <int> & find(string & key); If I do this, I get the obvious warning warning: reference to local variable 'lineNum' returned [enabled by default].
If I do vector<int> & find(string & key) const; I get a huge error that starts out like
In member function 'std::vector<int>& index_table::find(std::string&) const': indextable.cpp:74:30: error: no match for 'operator='
I want to build a server which holds hundreds of thousands of active users in memory. To keep all the users organized i would like to store them in a Vector.
The problem is how i could quickly and easy find the object whenever i need it? All users will have a unique ID. Would it be possible to keep some form of a Vector Index on the unique id number?