473,607 Members | 2,674 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing an iterator as an argument

Hi

I am trying to pass a iterator to a function, this is the iterator and
what it does

typedef vector<string>: :const_iterator vs_itr;
for(vs_itr i=vect.begin(); i!=vect.end(); ++i){
x = fix_it(*i, 2.0);
..

// fix_it(string* 1.235, 2.0) returns double 1.24
double fix_it(vector<s tring>::const_i terator s, double n){
double y = strtod(s->c_str(),0);
return floor( y*pow(10,n) + 0.5 ) / pow(10,n);
}

I am getting an error relating to the type problem

*************** *************** *************** *************** ****
no matching function for call to
'gen_data::fix_ it(const std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char>
>&, double&)'
gen_data.h:27: note: candidates are:
double gen_data::fix_i t(__gnu_cxx::__ normal_iterator <const
std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char>
>*, std::vector<std ::basic_string< char, std::char_trait s<char>,
std::allocator< char, std::allocator< std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char >, double)
*************** *************** *************** *************** ****

I tried to fix it by redefining the first argument of the function so
that it is a const vector<string>: :const_iterator but that did not fix
it.
Aug 8 '06 #1
2 6619

Gary Wessle wrote:
Hi

I am trying to pass a iterator to a function, this is the iterator and
what it does

typedef vector<string>: :const_iterator vs_itr;
for(vs_itr i=vect.begin(); i!=vect.end(); ++i){
x = fix_it(*i, 2.0);
Should be fix_it(i, 2.0), no?
..

// fix_it(string* 1.235, 2.0) returns double 1.24
double fix_it(vector<s tring>::const_i terator s, double n){
double y = strtod(s->c_str(),0);
return floor( y*pow(10,n) + 0.5 ) / pow(10,n);
}

I am getting an error relating to the type problem

*************** *************** *************** *************** ****
no matching function for call to
'gen_data::fix_ it(const std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char>
&, double&)'

gen_data.h:27: note: candidates are:
double gen_data::fix_i t(__gnu_cxx::__ normal_iterator <const
std::basic_stri ng<char, std::char_trait s<char>, std::allocator< char>
*, std::vector<std ::basic_string< char, std::char_trait s<char>,
std::allocator< char, std::allocator< std::basic_stri ng<char,
std::char_trait s<char>, std::allocator< char >, double)
*************** *************** *************** *************** ****

I tried to fix it by redefining the first argument of the function so
that it is a const vector<string>: :const_iterator but that did not fix
it.
Aug 9 '06 #2
In article <87************ @yahoo.com>, Gary Wessle <ph****@yahoo.c am>
wrote:
Hi

I am trying to pass a iterator to a function, this is the iterator and
what it does

typedef vector<string>: :const_iterator vs_itr;
for(vs_itr i=vect.begin(); i!=vect.end(); ++i){
x = fix_it(*i, 2.0);
..

// fix_it(string* 1.235, 2.0) returns double 1.24
double fix_it(vector<s tring>::const_i terator s, double n){
double y = strtod(s->c_str(),0);
return floor( y*pow(10,n) + 0.5 ) / pow(10,n);
}
The best solution would be:

double fix_it( const string& s, double n ) {
double y = strtod( s.c_str(), 0 );
return floor( y * pow( 10, n ) + 0.5 ) / pow( 10, n );
}

In other words, don't pass an iterator to a function unless you need to
features that the iterator provides.

If you must do so, then:

for ( vs_itr i = vect.begin(); i != vect.end(); ++i ) {
x = vix_it( i, 2.0 );
// note the asterisk isn't in the above line.
Aug 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
3658
by: Grant Edwards | last post by:
In an interview at http://acmqueue.com/modules.php?name=Content&pa=showpage&pid=273 Alan Kay said something I really liked, and I think it applies equally well to Python as well as the languages mentioned: I characterized one way of looking at languages in this way: a lot of them are either the agglutination of features or they're a crystallization of style. Languages such as APL, Lisp, and Smalltalk are what you might call style...
8
9127
by: Giulio | last post by:
I'm doing a class who takes in constructor a vector<int> keeps and makes some statistic on the vector whitout modifiing it. I would like not to make a copy of the vector for the class. to let the full process be faster a smaller in memory. I tried to pass the vector by reference using * but that operator doesn't exist. my ask is: how the vector are passed to functions?
1
1503
by: Martin Magnusson | last post by:
I'm writing a custom iterator which works on a specific class and needs access to some of this class' private data. The iterator class is a friend of the other class, but I suppose that the iterator also needs a pointer to the object over which it iterates. My question is: what's the easiest way to supply this pointer? It seems slightly cumbersome to have to do something like Vertex_Iterator i( this ); everytime I create a new...
5
1963
by: Active8 | last post by:
vector<double> signal; vector<double>::iterator iter; in_file.load_vector(signal); in_file.load_vector(signal.begin()); those calls give the compiler error: "could not deduce template argument for 'T'" the infile object is a non-template class with a template function
3
2371
by: sd2004 | last post by:
I am still learning, could someone show/explain to me how to fix the error. I can see it is being wrong but do not know how to fix. could you also recommend a book that I can ref. to ? ////////////////// ERROR MESSAGE ///////////////////// bash-2.05b$ g++ test5c.cpp test5c.cpp: In function `int main()': test5c.cpp:36: error: invalid initialization of reference of type
1
10888
by: Neo | last post by:
hi, This is my first post in C++ group, so please be nice to me. Thanks. Also, I will post my first C++ program following. There is a compile error, I cannot figure it out even I can fix it. And please give me your comments on my program as much as you can. I think I need some basic programming convention of C++. BTW, I used C a lot before, so if you can explain me from low-level view that would be great!
4
2588
by: lutorm | last post by:
Hi all, I'm having a problem writing template functions that take vector<T>::iterator as arguments and I'm sure you guys can set me straight. Like this: #include<vector> using namespace std; template<typename T> void test2(typename std::vector<T>::iterator b)
0
1928
by: wellingj | last post by:
A little back ground on what I'm trying to do: I'm making a generic weighted graph class (vertexes and edges althought I don't call them that) to implement some pathfinding algorithms like A* and D*. I am also going to compare a grid map to a hex map, which is why I want to make a generic base graph class that gridmap and hexmap will inherit from. so here is the error I'm getting: TwoDMap.cpp: In member function 'void TwoDMap::setArc(const...
15
2398
by: puzzlecracker | last post by:
I see that a lot of former in the code, and wonder if there is a technical reason for that
0
8049
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8463
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8322
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6803
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5471
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3953
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2461
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1574
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1316
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.