This example is not about a constructor. I would not try to understand it at this point.
But, you don't have a book? You should first read up some chapter on constructors. Then you could make better progress.
Printable View
This example is not about a constructor. I would not try to understand it at this point.
But, you don't have a book? You should first read up some chapter on constructors. Then you could make better progress.
Hello Elysia,
I don't have a book.
All the information I have can be found at this site en other sites.
Roelof
Edit 1 :
Is this good information : http://www.fredosaurus.com/notes-cpp...structors.html
Or is this better : http://cplus.about.com/od/learning1/ss/constructors.htm
I suggest you get one. You cannot learn the language on tutorials and webpages alone.
See the books thread. I can recommend Accelerated C++ myself.
Your local library might have some programming book available, too, if you're feeling cheap.
Nope. In fact, classcomp is a primitive class (C++ classes are really structs with a different label on them). It could have been written like this:
This is then used as the comparison function for the map:Code:class classcomp {
public:
bool operator() (const char& lhs, const char& rhs) const {
return lhs<rhs;
}
};
This determines how the elements are sorted internally. Try this:Code:map<char,int,classcomp> fourth;
Notice I switched < to > in the compare function, so this prints out:Code:#include <iostream>
#include <map>
using namespace std;
class classcomp {
public:
bool operator() (const char& lhs, const char& rhs) const {
return lhs>rhs;
}
};
int main() {
map<char,int,classcomp> test;
map<char,int>::iterator it;
test['a'] = 50; // the int values are irrelevant in this program
test['b'] = 50;
test['c'] = 200;
it = test.begin();
while (it != test.end()) {
cout << it->first << endl;
it++;
}
return 0;
}
c
b
a
I also very strongly recommend you get a beginner C++ book roelof. Your life will be much easier. You can probably find something used on Amazon for <$10 US.
Hello,
I have asked the library for the Accelared C++ book.
So i have to wait if they find it.
Roelof
Hello,
I have found a pf version of this book and have read chapter 0.
Two questions.
1) Is it still necessarly to put std:: before cout ?
2) How can I see if I did the exercises the right way ?
Roelof
You must do either of three things:
1) Use std::cout.
2) Put using std::cout; at the top of your file and then use cout.
3) Put using namespace std; at the top of your file and then use cout.
(But do NOT use a using directive in a header file.)
oke,
And do you have a answer to 2).
Roelof
Hmmm. I don't have that book lying around, so unfortunately, I do not know.
But there ought to some kind of answer section.
Hello Elysia.
The only thing I can find it this site : Accelerated C++
But there is only the source code and not the answers to the other questions.
Roelof