|
Gainer
Syzurp



Registered: 10/20/04
Posts: 1,468
Loc: Ðirty §outh
Last seen: 8 days, 14 minutes
|
Anyone really good at C++ and using STL I need some help
#7991939 - 02/07/08 02:24 PM (15 years, 11 months ago) |
|
|
Code:
#include <cstdlib> #include <iostream> #include <fstream> #include <vector> #include <map> #include <string> #include <iterator> #include <algorithm>
using namespace std;
int counter = 0; void count(string &s);
int main(int argc, char *argv[]){
string from, to;
cin >> from; ifstream is(from.c_str()); istream_iterator<string> ii(is); istream_iterator<string> eos; vector<string> b(ii, eos); sort(b.begin(), b.end()); ofstream os(to.c_str());
for_each(b.begin(), b.end(), count);
return 0; }
void count(string &s){ for(int i = 0; i < s.length();i++){ if(s[i] == 'e' || s[i] == 'E'){ counter++; } } }
This line is giving me problems. for_each(b.begin(), b.end(), count);
It should step through the vector and pass each string to the count function but the compiler always spits up this crap:
eCount.cpp: In function âint main(int, char**)â: eCount.cpp:32: error: no matching function for call to âfor_each(__gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, __gnu_cxx::__normal_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >, <unresolved overloaded function type>)â
Any help would be much appreciated.
-------------------- "I mean, it's like a koala bear crapped a rainbow in my brain!" -Captain Murphy "Quit being a bitch and pill me up" -Dr. Quinn "Smoke that bitch" "I am not Stormy, I am He who smokes Bitches!" -Stormy
|
Gainer
Syzurp



Registered: 10/20/04
Posts: 1,468
Loc: Ðirty §outh
Last seen: 8 days, 14 minutes
|
Re: Anyone really good at C++ and using STL I need some help [Re: Gainer]
#7994362 - 02/07/08 11:04 PM (15 years, 11 months ago) |
|
|
Bump anyone?... Or anyone know of a good C++ forum.. No I'm not that lazy googling it now just wondering if anyone has any suggestions.
-------------------- "I mean, it's like a koala bear crapped a rainbow in my brain!" -Captain Murphy "Quit being a bitch and pill me up" -Dr. Quinn "Smoke that bitch" "I am not Stormy, I am He who smokes Bitches!" -Stormy
|
Boom
just a tester

Registered: 06/16/04
Posts: 11,252
Loc: Cypress Creek
|
Re: Anyone really good at C++ and using STL I need some help [Re: Gainer]
#7994437 - 02/07/08 11:32 PM (15 years, 11 months ago) |
|
|
shouldn't you be sending an ostream to the function "count"?
something like:
for_each(b.begin(), b.end(), count(os));
|
Boom
just a tester

Registered: 06/16/04
Posts: 11,252
Loc: Cypress Creek
|
Re: Anyone really good at C++ and using STL I need some help [Re: Boom]
#7994443 - 02/07/08 11:34 PM (15 years, 11 months ago) |
|
|
wait nevermind
I'll think about it
|
Boom
just a tester

Registered: 06/16/04
Posts: 11,252
Loc: Cypress Creek
|
Re: Anyone really good at C++ and using STL I need some help [Re: Boom]
#7994459 - 02/07/08 11:39 PM (15 years, 11 months ago) |
|
|
For one thing though, I'd get rid of the & in the functions...you don't need to be passing it in like that, do you?
|
Gainer
Syzurp



Registered: 10/20/04
Posts: 1,468
Loc: Ðirty §outh
Last seen: 8 days, 14 minutes
|
Re: Anyone really good at C++ and using STL I need some help [Re: Boom]
#7994467 - 02/07/08 11:42 PM (15 years, 11 months ago) |
|
|
I've tried it without gives same error message. I was just passing by reference cause thats what our teacher was doing in all his examples.
-------------------- "I mean, it's like a koala bear crapped a rainbow in my brain!" -Captain Murphy "Quit being a bitch and pill me up" -Dr. Quinn "Smoke that bitch" "I am not Stormy, I am He who smokes Bitches!" -Stormy
|
Boom
just a tester

Registered: 06/16/04
Posts: 11,252
Loc: Cypress Creek
|
Re: Anyone really good at C++ and using STL I need some help [Re: Gainer]
#7994474 - 02/07/08 11:44 PM (15 years, 11 months ago) |
|
|
you have to use the for_each function? That's some ugly syntax
|
Gainer
Syzurp



Registered: 10/20/04
Posts: 1,468
Loc: Ðirty §outh
Last seen: 8 days, 14 minutes
|
Re: Anyone really good at C++ and using STL I need some help [Re: Boom]
#7994536 - 02/08/08 12:10 AM (15 years, 11 months ago) |
|
|
Yeah I think so he's trying to get us good at using the STL and iterators. I could just write a loop to do it much easier.. I'm not sure though I think he only specified that we had to read in the file using iterators might be able to get away with just writing a loop.
-------------------- "I mean, it's like a koala bear crapped a rainbow in my brain!" -Captain Murphy "Quit being a bitch and pill me up" -Dr. Quinn "Smoke that bitch" "I am not Stormy, I am He who smokes Bitches!" -Stormy
|
Gainer
Syzurp



Registered: 10/20/04
Posts: 1,468
Loc: Ðirty §outh
Last seen: 8 days, 14 minutes
|
Re: Anyone really good at C++ and using STL I need some help [Re: Gainer]
#7994586 - 02/08/08 12:25 AM (15 years, 11 months ago) |
|
|
Code:
//CSC 317 //eCount.cpp
#include <cstdlib> #include <iostream> #include <fstream> #include <vector> #include <map> #include <string> #include <iterator> #include <algorithm>
using namespace std;
int counter = 0; void count(string &s);
int main(int argc, char *argv[]){
string from, to; int main(int argc, char *argv[]){
string from, to;
cin >> from; ifstream is(from.c_str()); istream_iterator<string> ii(is); istream_iterator<string> eos;
vector<string> b(ii, eos); sort(b.begin(), b.end()); ofstream os(to.c_str());
//for_each(b.begin(), b.end(), count); for(vector<string>::iterator s = b.begin(); s != b.end();++s){ count(*s); }
cout << "There are: " << counter << " e's." << endl;
return 0; }
void count(string &s){
for(int i = 0; i < s.length();i++){ if(s[i] == 'e' || s[i] == 'E'){ counter++; } } }
This code works fine yeah.. its some kinda ugly code right now got those global variables floating around in there fix that later. But anyway still can't figure out why the for_each() version doesn't work it shouldn't have any problems.
-------------------- "I mean, it's like a koala bear crapped a rainbow in my brain!" -Captain Murphy "Quit being a bitch and pill me up" -Dr. Quinn "Smoke that bitch" "I am not Stormy, I am He who smokes Bitches!" -Stormy
|
|