I've just happen seen a pointer code and would like to ask someone to explain for me:
Thanks, i really appreciate.Code:typedef void* (*mFuncPtr) (char *args); mFuncPtr mFunc;
I've just happen seen a pointer code and would like to ask someone to explain for me:
Thanks, i really appreciate.Code:typedef void* (*mFuncPtr) (char *args); mFuncPtr mFunc;
Hello, testing testing. Everthing is running perfectly...for now
This declares a new type mFuncPtr which is a pointer to a function which looks like "void* foo (char* args)". You have to know that functions have a location in memory; thus, they can be pointed to.
Code:typedef void* (*mFuncPtr) (char* args); mFuncPtr mFunc = 0; void* my_foo (char* args) { printf("%s in my_foo", args); return 0; } mFunc = my_foo; (*mFunc)("I am"); // Ouputs "I am in my_foo"
or you can call it in simpler form:Originally Posted by Desolation
Code:mFunc("I am");
Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem
So it's pretty much like copy the string from "my_foo" and add that string to "mFunc"
Hello, testing testing. Everthing is running perfectly...for now
Not at all. The pointer holds the address of the function my_foo. When dereferencing a pointer you get the value to which it points to, right ? Well that value is the my_foo in this case so when I dereference it, I can pass it the arguments required by the function.
Here's a link pointing (pun intented) to an article I've written on basic STL usage and there is a good example of how function pointers can be used. Enjoy.
http://code-dynasty.net/articles/C_A...std::map%3C%3E
I have that link saved ever since I went to that website for the first time.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Nice ! Don't be afraid to post there as well, we're moving from a forum that's dying but we're looking for new members as well. =]
I've got issues with that link.
You can add to the front (or middle) of a vector since it supports the insert member function (although adding to the back is certainly more efficient). You can also remove elements from the vector at any position using the erase member function.Originally Posted by http://code-dynasty.net/articles/C_And_CPP_Programming/80#std::map%3C%3E
Later on, a nit:
The "destructor" is missing the '~'. It might also help to put in the instantiation of the static data member that is missing from that example (to help avoid any confusion).Originally Posted by http://code-dynasty.net/articles/C_And_CPP_Programming/80#std::map%3C%3E
Concerning the list container:
As mentioned, the vector container also supports the erase member function. Also note that what is discussed concerning the erase member function is only one version of the function which is overloaded to support two different forms, one with a single argument and the other with the two arguments noted (it may be worth at least mentioning).Originally Posted by http://code-dynasty.net/articles/C_And_CPP_Programming/80#std::map%3C%3E
Later, concerning maps:
The step in red above is unnecessary. You do not need to initialize the values to 0.Code:// This is how you declare a map // The unique identifier is the character and // the value is its number of occurence std::map<char, int> c_freq; for(int i = 0; i < 256; i++) // We need to set each counter to 0. c_freq[(char)i] = 0; char text[] = "This is a test string, it is not meaningful"; // Here we increment the counter of each character // encountered simply by using the key for(int i = 0; text[i] != '\0'; i++) // (text[i]) is the current character in the string // c_freq[(text[i])] returns a reference to the counter of the character c_freq[(text[i])]++;
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
I will make the appropriate changes, I have to disagree with the last bit, unless there is something I am not aware of. I am incrementing the counters but if I don't initialize them, I will get an error saying I am using uninitialized variables. This is unless they are set by default to 0 which I highly doubt because it could break some code like:
Code:class Foo { public: Foo() { } explicit Foo(const Foo&) { } }; std::map<int, Foo> myMap; // error, cannot convert from 0 to const Foo
In such a case, primitive types are zero initialised, class types are default initialised.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Very interesting ! Thanks !
There is a deeper misconception here, methinks. std::remove (and std::remove_if) do not remove from the container, but from a range designated by an iterator pair. In the end, the member erase function must be called to actually remove the 'removed' elements from the container.This means that you cannot remove an element at the front using the std::vector<> 's built-in functions. You will have to use the function std::remove() to do so but it's not the best solution.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)