how to insert data in a map container

This is a discussion on how to insert data in a map container within the C++ Programming forums, part of the General Programming Boards category; This example is not about a constructor. I would not try to understand it at this point. But, you don't ...

  1. #31
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #32
    Registered User
    Join Date
    May 2010
    Posts
    230
    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
    Last edited by roelof; 06-10-2010 at 06:01 AM.

  3. #33
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #34
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roelof View Post
    I see that classcomp for me is teamstats.
    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:
    Code:
    class classcomp {
        public:
            bool operator() (const char& lhs, const char& rhs) const {
                return lhs<rhs;
            }
    };
    This is then used as the comparison function for the map:
    Code:
    map<char,int,classcomp> fourth;
    This determines how the elements are sorted internally. Try this:
    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;
    }
    Notice I switched < to > in the compare function, so this prints out:

    c
    b
    a


    Quote Originally Posted by roelof View Post
    All the information I have can be found at this site en other sites.
    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.
    Last edited by MK27; 06-10-2010 at 06:10 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #35
    Registered User
    Join Date
    May 2010
    Posts
    230
    Hello,

    I have asked the library for the Accelared C++ book.
    So i have to wait if they find it.

    Roelof

  6. #36
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  7. #37
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #38
    Registered User
    Join Date
    May 2010
    Posts
    230
    oke,

    And do you have a answer to 2).

    Roelof

  9. #39
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    Hmmm. I don't have that book lying around, so unfortunately, I do not know.
    But there ought to some kind of answer section.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #40
    Registered User
    Join Date
    May 2010
    Posts
    230
    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

  11. #41
    Registered User
    Join Date
    Jun 2010
    Location
    Bakersfield, California
    Posts
    22
    Quote Originally Posted by roelof View Post
    ... chapter 0 ...
    I really got a kick out of that... only a C book...

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding the Linker Map File
    By Hops in forum C Programming
    Replies: 0
    Last Post: 01-14-2010, 04:40 PM
  2. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  3. data structure design for data aggregation
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-20-2008, 06:43 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21