Could someone please tell me why the code below not compile ?
Thanks in advance for your help.
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
void split (const string& s, char c){
vector<string> v;
string:: size_type i=0;
float data;
string:: size_type j=s.find(c);
while (j!=string::npos){
v.push_back(s.substr(i,j-i));
i = ++j;
j=s.find(c,j);
if (j==string::npos)
v.push_back(s.substr(i,s.length()));
}
vector<string>::iterator pos;
pos = find (v.begin(),v.end(),2);
cout << "pos: " <<*pos <<endl;
}
int main (){
double results;
char const delim = ';';
string s("id;2;address;4;5;6;7");
split (s,delim);
} 4 1386
sd2004 wrote: vector<string>::iterator pos; pos = find (v.begin(),v.end(),2);
^^^^^
Are you expect find will do implicit conversion from int (number 2)
to string?
Read about std::find algorithm:
third parameter is the value to be searched for.
So, if you are searching through vector of strings then you
have to search against string.
In example:
pos = find (v.begin(), v.end(), "2");
Cheers
--
Mateusz Łoskot http://mateusz.loskot.net
sd2004 wrote: Could someone please tell me why the code below not compile ? Thanks in advance for your help.
Please post the text of error messages, and indicate the line on which
they occur. Saves the rest of us the trouble, so we can help you more
efficiently. We like efficiency! ;)
Luke
I changed the code a little bit.
I now compile , however , the program is running into an endless loop.
Could someone please help ?
Perhap, show me a better way of coding .
Thank in advance for your time.
#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include <algorithm>
using namespace std;
//void split (const string& s, char c,const int here){
float split (const string& s, char c, const int here){
vector<string> v;
string:: size_type i=0;
float data;
string:: size_type j=s.find(c);
while (j!=string::npos){
v.push_back(s.substr(i,j-i));
i = ++j;
j=s.find(c,j);
if (j==string::npos)
v.push_back(s.substr(i,s.length()));
}
for (vector<string>::size_type f=0;f!=v.size();++f)
if (f=here){
cout << "here: "<< here<<endl;
data = atof(v[f].c_str());
}
return data;
}
int main (){
double results;
char const delim = ';';
int loc =3;
string s("Account;124.5;1.0;1323.31;333.2");
// split (s,delim);
results= split (s,delim,loc);
cout << "RESULTS IS: "<<results<<endl;
}
sd2004 wrote: I changed the code a little bit. I now compile , however , the program is running into an endless loop. Could someone please help ?
Please, let me to recommend you a book which I hope will be very helpful.
"Accelerated C++" , A. Koenig and B. Moo
Review: http://www.accu.org/bookreviews/publ.../a/a002212.htm
Look at chapter 5.6 and you will find implementation of string split
function with very clear explanation of all "whats" and "whys" related
to the solution.
Cheers
--
Mateusz Łoskot http://mateusz.loskot.net This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
by: Michael Klatt |
last post by:
I am trying to write an iterator for a std::set that allows the
iterator target to be modified. Here is some relvant code:
template <class Set> // Set is an instance of std::set<>
class...
|
by: nick |
last post by:
Hi,
I need to manage a "layered" collection of objects, where each layer
grows independently, e.g,
o--+--+--+--+--+ 1st layer
|
o 2nd layer (empty)
|
o--+--+--+ 3rd...
|
by: shawnk |
last post by:
I searched the net to see if other developers have been looking for a
writable iterator in C#. I found much discussion and thus this post.
Currently (C# 2) you can not pass ref and out arguments...
|
by: mailforpr |
last post by:
Hi.
Let me introduce an iterator to you, the so-called "Abstract Iterator"
I developed the other day.
I actually have no idea if there's another "Abstract Iterator" out
there, as I have never...
|
by: T.A. |
last post by:
I understand why it is not safe to inherit from STL containers, but I have
found (in SGI STL documentation) that for example bidirectional_iterator
class can be used to create your own iterator...
|
by: mailforpr |
last post by:
How do I do that?
The thing is, the only information I have about the iterator is the
iterator itself. No container it is belonging to or anything. Like
template<Iteratorvoid...
|
by: Steven D'Aprano |
last post by:
I thought that an iterator was any object that follows the iterator
protocol, that is, it has a next() method and an __iter__() method.
But I'm having problems writing a class that acts as an...
|
by: mkborregaard |
last post by:
Hi,
I have the weirdest problem, and I can not see what is going wrong.
I have made a 2d container class, and am implementing an iterator for that class. However, the ++ operator is behaving very...
|
by: Terry Reedy |
last post by:
Luis Zarrabeitia wrote:
Interesting observation. Iterators are intended for 'iterate through
once and discard' usages. To zip a long sequence with several short
sequences, either use...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
|
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 projectplanning, coding, testing,...
| |