C++ :: Find Function To A Vector Of Structures Vector

Jul 5, 2013

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:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

[Code] ....

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.

View 4 Replies


ADVERTISEMENT

C++ :: Function To Find Out Iterator Of Certain Value In Vector

Sep 4, 2013

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.:

vector<tIndex_t>::iterator unlabelTit(const tIndex_t index) {
for(vector<tIndex_t>::iterator it=unlabelT.begin(); it<unlabelT.end(); it++) {
if(index==*it) return it;
} return NULL;
}

But looks this one is not accepted by compiler, saying I cannot do this kind of comparison like:

unlabelTit(i)!=NULL;

so I just delete the return NULL; and then the compiler giving me warning about no return statement before }.

a pointer pointing to nonsense? how to do that?

View 3 Replies View Related

C++ :: Find Function - Return Reference To A Vector

Oct 12, 2014

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='

Am I using the const identifier incorrectly?

View 5 Replies View Related

C++ :: Using Vector Push Back Function To Output Contents Of Vector (similar To Array)

Feb 9, 2015

How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.

here is my code:

#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)

[Code].....

View 3 Replies View Related

C++ :: Recursive Function 2 - Create Vector Of Vector Of Integers

Mar 26, 2013

Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>

How do I make a function that creates vector of vector of every different integers?

<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>

and so on...

View 2 Replies View Related

C/C++ :: Implementation Of A Vector Of Structures

Apr 14, 2014

I created a structure Vector and implement some functions to make the new defined type (vector) dynamically allocated and resized (inspired from the C++ implementation of the dynamic arrays : vector). I need to assign a structure to every vector element but I am not sure that I am doing it right:

here is the structure that I've defined:

typedef struct  {
    void** mem; // to make this parametrizable I want the void* to point to a Structure (referenced with * a pointer)
    unsigned long     elems;
    unsigned long     elemsize; //element size

[Code] ....

I believe I have serious problems with pointers, values and dereferencing a pointer.

View 7 Replies View Related

C++ :: Find All Possible N Combination In Vector

Aug 10, 2013

For example let say there is vector [1,2,3,4,5,6,7,8,9] and let say there is int n

if i say n=3
I want to find all possible 3 combination
[1,2,3][1,2,4].....such and such

if i say n=4
[1,2,3,4][1,2,3,5]......

I want to know how you write the code depend on the input n. Is it call dynamic programming?

View 4 Replies View Related

C++ :: Find String In Vector?

Oct 15, 2013

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.

View 2 Replies View Related

C++ :: Find Mode Of Vector Through Map?

Feb 16, 2015

Im currently trying to find the mode of a vector through a map but am not sure how to do so besides the attempt ive made

void mode(vector<double>v)
{
map<double, int> map;

[Code]....

View 3 Replies View Related

C++ :: For Loop To Find Vector Length

Jul 21, 2013

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.

View 1 Replies View Related

C++ :: Find Object In Vector And Delete?

Dec 14, 2014

I have this class I have been working with,

Code:

// 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);
}
}

View 4 Replies View Related

C++ ::  Sort / Find Duplicates And Remove From Vector

Nov 18, 2013

I'm trying to sort my vector, find duplicate values using unique and erase them. Program can compile successfully but the duplicates are not removed.

output:

x: 3
y: 2
x: 6
y: 4
x: 3
y: 2
vector<Point2D> p2dvector;
void readData()

[Code].....

I also have operator== in my Point2D class which I understand it is required to use unique for vector.

bool operator==(const Point2D& lhs, const Point2D& rhs)
{
return lhs.x == rhs.y;
}

View 10 Replies View Related

C++ :: Huge Vector - Find Item Fast

Dec 31, 2012

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?

View 2 Replies View Related

C/C++ :: Word Maze - Find String In Vector

Mar 4, 2015

This program is to solve a word maze and find the number of words in the maze from a dictionary file.

The program runs, but stops by finding 1,2 and 3 letter words.

#include<iostream>
#include<vector>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;
int main() {
vector <string> dict,forward,reverse;

[Code] .....

I also tried to find words using the entire vector as a container, the synopsis code that also did not work is below

while(j<dict.size()) {
if(dict[j].size()>2) {
if (std::find(forward.begin(), forward.end(), dict[j]) != forward.end()) {
cout<<"found word "<<dict[j]<<endl;

[Code] ....

Attached File(s)

dumpling2.txt (2.54K)

Dictionary.txt (718.34K)

View 13 Replies View Related

C++ :: Open File And Read In Rows To String Vector And Return Vector

Jun 7, 2012

I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,

N,N'-dimethylethylenediamine.mol

This is the function that opens the file :

Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );

[Code] ....

The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.

View 9 Replies View Related

C++ :: Create Class Vector As Template And Define Operations On Vector?

May 13, 2013

I need to create a class vector as a template and define operations on vectors.

And this is what I made.

#include<iostream>
using namespace std;
template<class T>

[Code].....

View 2 Replies View Related

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:

#3.57 3.18#
#84.91 27.69
#93.40 4.62
#67.87 9.71
#75.77 82.35
#74.31 69.48
#39.22 31.71
#65.55 95.02
#17.12 3.44
#70.60 43.87

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;

`struct point{
int x;
int y;

[Code] ....

View 1 Replies View Related

C/C++ :: Cannot Print Vector Of Vector Of Doubles

Mar 31, 2014

I am trying to print a matrix solution that I've stored in a vector of doubles. The original matrix was stored in a vector of vector of doubles. I want to print to the screen and an output file. Right now it just prints to screen column-style and the output file is horizontal-style. I tried to change the solution from vector of doubles to a vector of vector of doubles like the original matrix but when I run my code it crashes. Here is what I am referring to:

void readMatrix(vector<vector<double>> &matrix, vector<double> &b, int &N, string input) {
ifstream in(input);
in >> N;
matrix.resize(N);
b.resize(N);

[Code] ....

However when I change my printResult function to this (I removed the string argument because I just want to get it working to the screen first):

void printResult(vector<vector<double>> &sol, const int N) {
//ofstream out(output);
//int j;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++){

[Code] ....

The program crashes. Am I even printing the matrix correctly?

View 4 Replies View Related

C/C++ :: Swapping Vector Int And Vector Strings

Mar 30, 2013

I want to have it so that when i ask for the person witch item they want to drop on the ground it goes into another vector that i can pick back up the item if they want it back and erase when they walk away.

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cmath>  
using namespace std;  
    struct InventoryItem

[Code] ....

View 4 Replies View Related

C++ :: Can't Read In Matrix Into Vector Of Vector Of Int

Mar 24, 2012

Code:
#include <iostream>
#include <istream>
#include <fstream>
#include <vector>

[Code]....

Why is it reading in nothing for the arrays, and also making the size of the total thing the total number of numbers? It should have a size of 2, not 5.

View 14 Replies View Related

C++ :: Copy Part Of Vector 1 To Vector 2

Jun 21, 2012

I am trying to copy vector to vector as follow:

Code:

std::vector<unsigned char> vec1;
//insert some values into vec1
std::vector<unsigned char> vec2;

Now I want to to copy 2 bytes from vec1 starting at index 5., why do i need to know how many bytes from the end of vec1?? can't i just specify how many bytes i want to copy from starting index?

std::copy(vec1.begin()+5, vec1.end()-??, vec2.begin());

View 2 Replies View Related

C++ :: Using A Referenced Vector In A Function

Jun 22, 2013

tell me what I am doing wrong here?

Code: void print(const std::vector<int>& vec)
{
for ( std::vector<int>::iterator itr = vec.begin(), end = vec.end(); itr != end; ++itr )
{
std::cout << *itr << std::endl;
}
}

View 4 Replies View Related

C++ :: Passing A Vector To A Function

Nov 3, 2013

i'm trying to send a vector to a function however i am getting these errors

error: invalid initialization of non-const reference of type 'std::vector<sf::Texture>&' from an rvalue of type 'std::vector<sf::Texture> (*)(sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::Texture, sf::T|

error: in passing argument 1 of 'int set_explosion_textures(std::vector<sf::Texture>&)'|

my relevant code is

//vector containing fighter explosion frames
std::vector<sf::Texture> f_explosion_textures(sf::Texture f_explosion_1, sf::Texture f_explosion_2, sf::Texture f_explosion_3,

[Code].....

View 7 Replies View Related

C/C++ :: Function Isn't Returning A Vector

Nov 11, 2014

I am writing a function to take two vectors and put them end to end in a third vector. I'm new to working with vectors, and I cannot figure out why my append function is not returning vector C. I had the function print out vector C within it to make sure the logic in the function wasn't the problem, and it worked perfectly. My code is as follows:

#include <iostream>
#include <vector>
using namespace std;
//append function to put vector b after vector a in vector c
vector <int> append(vector <int> a, vector <int> B)/>/>/> {
vector <int> c;

[code]....

and my output is as follows:

Vector A contains: 10 18 123 172
Vector B contains: 283 117 17

The two vectors back to back are:

Obviously, the third vector is not returning from the function to main properly, but why.

View 5 Replies View Related

C++ :: How To Use Function That Returns Vector

Oct 7, 2014

How can I show elements of a vector that is returned by a function in main() ?

Code:
vector<int> merge_sorted(vector<int> a, vector<int> b){
vector<int>temp;
int count = 0;
if(a.size() < b.size()) count = a.size();
else count = b.size();

[Code] .....

View 12 Replies View Related

C++ :: Increase Sizes Of Queue In A Vector Of Queues And Find Shortest Queue

Feb 20, 2013

I have a paradigm in a loop of queues of a vector,if a condition is true,increase sizes of the queue of that particular queue in the loop of queues, if condition is false, the queuesize is left as such in loop of queues. After this operation i need to search the queue sizes of all queues and enqueue in the shortest queue.

I want to do something like the code given below

#include <vector>
#include <queue>
int min_index = 0;
std::vector<std::queue<int> > q
std::size_t size = q.size();

[Code] ....

How to implement this logic?

will q[i].size=q[i].size+5 increase the queuesize by 5 for the ith queue?

View 12 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved