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


ADVERTISEMENT

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 View Related

C++ :: Get Iterator Position After Find If

Jan 25, 2013

I want to get the iterator position after to use find if:

std::list<Texture*>::iterator result = find_if(
texturelist.begin(),
texturelist.end(),
std::bind2nd<CompareTEX>(CompareTEX(),n_tex));
if (result != texturelist.end()) {
return // position result
}

View 5 Replies View Related

C++ :: Vector Iterator Not Incrementable

May 25, 2013

I am receiving the error: Vector Iterator not incrementable. However, when erasing I'm already re-setting 'it' and pre-incrementing at the end of the while-clause.what's wrong?

vector<st>::iterator it = map[0][0].begin();
while(it != map[0][0].end()) {
if((*it).val == 5) {

[Code] .....

View 1 Replies View Related

C/C++ :: Storing Map Iterator In Vector?

Jan 10, 2013

I'm trying out the code below; it runs and produces the output I'd expect, but could it lead to undefined behavior?

To test with, I show the key and wait for user input; if the input doesn't match the value, the iterator is stored in the vector.

map<string, string> test;
map<string, string>::iterator it;  
vector<map<string, string>::iterator> bad_input;  
string input;  
test["key 1"] = "value 1";

[Code] ....

I am also interested in knowing if I can use an iterator to walk through the vector? I tried

vector<map<string, string>::iterator>::iterator v_it;  
for(v_it = bad_input.begin(); v_it != bad_input.end(); ++v_it)
    cout << v_it-> ??  

However, I couldn't figure out how to access "first" and "second" using this method. Is it possible to do so?

View 2 Replies View Related

C++ :: Iterator To A Vector Of Struct Type?

Nov 18, 2013

I'm working on a program where I have a vector full of <myClassType> structs.

I'm trying to insert items into a vector, searching first through the vector to make sure the value isn't already in the vector before inserting it. The "find" function isn't working properly.

I keep getting C2678 "binary '==': no operator found which takes a left-hand operand of type "myClassType" or there is no conversion errors in Visual Studio 2010.

I know it's something having to do with the find function and my iterators, but I can't, for the life of me, figure out what it is.

I've tried switching to const_iterators, but I get the same error message.

void foo::insert(const myClassType& en) {
vector<myClassType>::iterator it;
vector<myClassType>::iterator it1;
it = find(Table.begin(), Table.end(), en.memberOne);

[Code] .....

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++ :: Specific Iterator Type - Find Distance By Simple Subtraction

Sep 24, 2014

I have a templated container that defines a forward iterator.

Calling std::distance on these iterators will generate code that will count the number of iterations it takes to get from the first parameter to the second, by repetitively incrementing.

Internally, the iterators can easily find the distance by a simple subtraction.

What I want to do is overload std::distance for these iterators so that it will take advantage of the simple calculation rather than repetitive increments.

The simple solution of course would be to make the iterators random access, but this would require that they support functionality that is not 'logical' for the container. Access to the container only makes logical sense when iterating one item at a time in the forward direction.

Code:
#include <iterator>
template <typename T>
class Container {
public:
class iterator : public std::iterator<std::forward_iterator_tag, T> {

[Code] .....

View 2 Replies View Related

C++ :: Translating Recursive Function Into Iterator Function

Nov 27, 2014

How do I change the following recursive function into a iterator function and basically what should it look like?

int f(int i) {
if(i<2)
return i;
return f(i-2)+f(i-1);
}

View 1 Replies View Related

C++ :: Changing Recursive Function Into Iterator Function?

Nov 29, 2014

I had the following question in my exam and received 3 out of a possible 4 marks

Here is the question:

Assume the following recursive function:
int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

Translate this function into an iterative version:

Here is my solution:

int f(int i)
{
int prev,next,final;
prev = 0;

[Code]....

View 9 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i)
{
int prev,next,final;
prev = 0;
next = 1;
if(i==1)
final = i;

[Code] .....

View 1 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function?

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i) {
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i) {
int prev,next,final;
prev = 0;
next = 1;

[Code] ....

View 1 Replies View Related

C++ :: Write A Template Which Defines Min Max Operators On Vectors - Iterator Function?

Jul 3, 2012

I have a .cpp file which I have to create a header file for. I started it but I have stuck and it is full of errors.

I have some tasks (see comments in the code):

Task 2: I have to write a template which defines min max operators on vectors, it must be a custom vector template. The main program only demonstrates that it creates a data structure which calls for min max operators.

Task 3: I need a special min max function which watches for any changes and it has to work lineally so it has to step along the elements of the vectors determining the min max values.

Task 4: I have to create an iterator function

Here is the main.cpp code:

Code:
#include <iostream>
#include "mmvec.h"
#include <algorithm>
#include <string>
#include "mmvec.h"
struct Limited {
int val;

[Code] ...

View 1 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++ :: 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++ :: 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++ ::  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++ :: 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++ :: 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

C/C++ :: Find The Address Of Function?

Oct 14, 2012

find the address of function? mail the ans on (email removed)

View 4 Replies View Related







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