tag:blogger.com,1999:blog-25013312809774239822018-02-13T03:56:31.267+05:30My Notepadstaraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.comBlogger132125tag:blogger.com,1999:blog-2501331280977423982.post-16931905422561157322016-12-12T09:35:00.000+05:302016-12-12T09:45:57.172+05:30Amazon Deal of the Day and Lightning Deals master link<div dir="ltr" style="text-align: left;" trbidi="on"><b>Master link</b> &#8667; <a href="http://www.amazon.in/gp/goldbox/ref=sr_deal_dotd" target="_blank">http://www.amazon.in/gp/goldbox/ref=sr_deal_dotd</a><br />
On the lift hand side you can see various categories like <b>Department</b>, <b>Deal Type</b>, <b>Availability</b>, <b>Price</b>, <b>Discount</b>, <b>Avg. Customer Review</b>. And on the right side you can see <b>Sort by</b> option. Select whichever you like.<br />
<b>Master ink for all items sorted by price</b> &#8667; <a href="http://www.amazon.in/gp/goldbox/ref=gbps_ftr_s-4_5fac_sort_PRCA?gb_f_GB-SUPPLE=includedAccessTypes:APP_ONLY_LD,sortOrder:BY_PRICE_ASCENDING&pf_rd_p=d68bfdc7-a64f-4ffe-a1b9-5267d0cb5fac&pf_rd_s=slot-4&pf_rd_t=701&pf_rd_i=gb_main&pf_rd_m=A1VBAL9TL5WCBF&pf_rd_r=11B460PTKGMTR1NPG112" target="_blank">http://www.amazon.in/gp/goldbox/ref=gbps_ftr_s-4_5fac_sort_PRCA?gb_f_GB-SUPPLE=includedAccessTypes:APP_ONLY_LD,sortOrder:BY_PRICE_ASCENDING&pf_rd_p=d68bfdc7-a64f-4ffe-a1b9-5267d0cb5fac&pf_rd_s=slot-4&pf_rd_t=701&pf_rd_i=gb_main&pf_rd_m=A1VBAL9TL5WCBF&pf_rd_r=11B460PTKGMTR1NPG112</a><br />
</div>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com1tag:blogger.com,1999:blog-2501331280977423982.post-14597769310748437222016-02-02T23:50:00.002+05:302016-02-02T23:51:16.145+05:30Nearbuy Referral CodeUse referral code WRTDAZ & get up to Rs.100 off on a first purchase on nearbuystaraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-41144751330617718302015-12-16T09:08:00.000+05:302015-12-16T09:32:21.483+05:30Little App Referral Code<div dir="ltr" style="text-align: left;" trbidi="on"><br />
Use referral code <b>5WCHR</b> & get 50% off on your first purchase: <a href="https://bnc.lt/m/48BuX6milp">https://bnc.lt/m/48BuX6milp</a><br />
<br />
<img src="http://i.imgur.com/pdcj5d6.jpg" /><br />
<br />
</div>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-48879333690861788382015-12-16T09:04:00.001+05:302015-12-16T09:04:06.996+05:30Peppertap Referral Code<div dir="ltr" style="text-align: left;" trbidi="on"><br />
Use <b>VMU9L</b> as a coupon code just before payment and you will get flat Rs.150 off on your first order.<br />
Order for Rs.250 or more to get free shipping. Which means add products to cart for Rs.250 or more and then apply the code to get Rs.100 off plus free shipping. The final amount after applying coupon can be less than Rs.250 to get free shipping. <br />
<br />
Peppertap decides which user to give referral codes. The only condition is that you must have placed at least one order. So, use the above mentioned referral code to place your first order. Then after that you will get your own referral code. If they decide to give you, you will get the link to the referral code at the bottom of the screen that says "<b>Refer & Earn Discounts!</b>". See the image below. If you tap on that you will get the referral code. The good news is, everyone will get it. Just wait for it.<br />
<br />
<img src="http://i.imgur.com/sZVf8yv.png" /><br />
<br />
</div>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-69551994624201667122015-05-14T07:45:00.005+05:302015-05-16T09:00:09.056+05:30Comparison of cashback websites<style>
th, td
{
width:100%;
}
</style>
<table>
<tr>
<th>&nbsp;</th><th><a href="http://www.topcashback.in" target="_blank">TopCashback</a></th><th><a href="http://in.linkis.in" target="_blank">LinKis</a></th><th><a href="http://cashkaro.com" target="_blank">CashKaro</a></th><th><a href="https://www.pogama.com" target="_blank">PoGaMa</a></th><th><a href="http://www.gopaisa.com" target="_blank">Gopaisa</a></th><th><a href="http://www.surecashback.com" target="_blank">SureCashback</a></th>
</tr>
<tr>
<td>Joining Bonus</td> <td>0</td> <td>0</td> <td>Rs.25</td> <td>Rs.50</td><td></td><td></td>
</tr>
<tr>
<td>Amazon</td> <td>1.51%-12.5%</td> <td>0%-10.01%</td> <td>10%</td> <td>0%-12.75%</td> <td>1.6%-12%</td> <td>3%-15%</td>
</tr>
<tr>
<td>Flipkart</td> <td>0.08%-12%</td> <td>0.08%-15%</td> <td>0%</td> <td>0.085%-11.9%</td> <td>0.8%-11%</td> <td></td>
</tr>
<tr>
<td>Paytm</td> <td>2.5%-6.5%</td> <td>0%-6%</td> <td>5.6%</td> <td>3.325%-3.8%</td> <td>2%-4%</td> <td>3%</td>
</tr>
<tr>
<td>Pepperfry</td> <td>10%-12%</td> <td>9%-11.25%</td> <td>8.25%</td> <td></td> <td>0%-10%</td> <td></td>
</tr>
<tr>
<td>Shopclues</td> <td>2%-5%</td> <td>1.88%-5.25%</td> <td>5.6%</td> <td>1.9%-5.225%</td> <td>2.1%-5.8%</td> <td>2.5%-5%</td>
</tr>
<tr>
<td>Snapdeal</td> <td>2.2%-9%</td> <td>1.88%</td> <td>2%-8%</td> <td>2.125%-8.5%</td> <td>2.1%-8.4%</td><td>2%-8%</td>
</tr>
</table>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-2704680816562524182015-02-26T12:36:00.004+05:302015-02-26T13:12:12.566+05:30Coupons<style>
/* Custom style to table elements START */
th, td
{
width:30%;
}
/* Custom style to table elements END */
</style>
<table>
<tr>
<th>Site</th><th>Coupon</th><th>T&C</th>
</tr>
<tr>
<td>Ebay</td><td>EBAYCHRN07</td><td>Buy Between<br>Rs.200 to Rs.500<br>Get Rs.100 Off<br><br>Buy Above<br>Rs. 500<br>Get Rs.200 Off</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYCHRN03</td><td>Buy Between<br>Rs.200 to Rs.500<br>Get Rs.100 Off<br><br>Buy Above<br>Rs. 500<br>Get Rs.200 Off</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYVAL005</td><td>Buy Between<br>Flat Rs.50 Off<br>Minimum Purchase : Rs.100</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYVAL005</td><td>Buy Between<br>Flat Rs.75 Off<br>Minimum Purchase : Rs.100</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYCHRN25</td><td>Buy Between<br>Flat Rs.150 Off<br>-Minimum Purchase: Rs.500</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYCHRN20</td><td>Buy Between<br>Rs.300 to Rs.500<br>Get Rs.200 Off<br><br>Buy Above<br>Rs. 500<br>Get Rs.300 Off</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYCHRN20</td><td>Buy Between<br>Flat Rs.75 Off<br>Minimum Purchase : Rs.100</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYSHOP33</td><td>10% OFF<br>Max Discount: Rs.3000</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYSHOP13</td><td>Rs 100 OFF<br>Min Purchase Rs. 200</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYSHOP12</td><td>Rs 100 OFF<br>Min Purchase Rs. 200</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYSHOP11</td><td>Rs 100 OFF<br>Min Purchase Rs. 200</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYSHOP08</td><td>Rs 100 OFF<br>Min Purchase Rs. 200</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYSHOP05</td><td>Rs 100 OFF<br>Min Purchase Rs. 200</td>
</tr>
<tr>
<td>Ebay</td><td>EBAYSHOP01</td><td>Rs 100 OFF<br>Min Purchase Rs. 200</td>
</tr>
<tr>
<td>Ebay</td><td>FESTIVE005</td><td>8% OFF<br>Min Purchase Rs. 200<br>Max Discount Rs. 4000</td>
</tr>
<tr>
<td>Ebay</td><td>FESTIVE001</td><td>8% OFF<br>Min Purchase Rs. 200<br>Max Discount Rs. 4000</td>
</tr>
</table>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-85735697296659503702015-01-12T11:40:00.002+05:302015-01-12T11:45:05.787+05:30Default location of Visual Studio project properties file &lt;Project> &rarr; Properties &rarr; Configuration Properties &rarr; VC++ Directories &rarr; Source Directories </br>
&lt;Project> &rarr; Properties &rarr; Configuration Properties &rarr; Debugging &rarr; Command Arguments </br>
%USERPROFILE%\AppData\Local\Microsoft\MSBuild\v4.0\Microsoft.Cpp.x64.user.props </br>
staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-631486851639658802014-09-14T14:58:00.000+05:302014-09-14T14:58:44.827+05:30C++ Contd..<p>
Templates
Mutex
BST
Linked List
Hash Function
Design Patterns
</p>
staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-84112827591909197172014-09-14T09:12:00.000+05:302014-09-14T10:50:07.923+05:30C++ Copy Constructor, Assignment Operator<p>
<b>Copy Constructor</b>
<pre class="brush:cpp">
Song(Song const&);
</pre>
It is needed if you want to initialize an object to take the value of another one:
<pre class="brush:cpp">
Song a;
Song b = a; // Calls copy constructor
</pre>
<b>Assignment Operator</b>
<pre class="brush:cpp">
void operator=(const Song&);
</pre>
It is needed for assigning a value to an already existing instance:
<pre class="brush:cpp">
Song a;
Song b;
b = a; // Calls assignment operator
</pre>
Note that the standard return value for an assignment operator is a reference:
<pre class="brush:cpp">
Song& operator=(const Song&);
</pre>
</p>
staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-62618134550995312332014-09-14T06:30:00.003+05:302014-10-13T12:08:01.504+05:30C++ Singleton<pre class="brush:cpp">
#include &lt;iostream>
using namespace std;
class Singleton
{
public:
static Singleton& getInstance() // See Note 1.
{
static Singleton instance; // See Note 2.
// Guaranteed to be destroyed.
// Instantiated on first use.
return instance;
}
void Print();
private:
Singleton() {}; // Constructor? (the {} brackets) are needed here.
// Dont forget to declare these two. You want to make sure they
// are unaccessable otherwise you may accidently get copies of
// your singleton appearing.
Singleton(Singleton const&); // Copy Constructor. Don't Implement
void operator=(Singleton const&); // Assignment Operator. Don't implement
};
void Singleton::Print()
{
cout << "I am a singleton" << endl;
}
int main(int argc, char *argv[])
{
Singleton &S1 = Singleton::getInstance(); // Create/Instantiate Singleton
S1.Print();
Singleton::getInstance().Print();
system("PAUSE");
return EXIT_SUCCESS;
}
</pre>
<p>
<b>Note 1.</b>
Why does everybody want to return a singleton as a pointer?
Returning it as a reference seems much more logical!
You should never be able to free a singleton manually. How do you know who is keeping a reference to the singleton? If you don't know (or can't guarantee) nobody has a reference (in your case via a pointer) then you have no business freeing the object.
<b>Note 2.</b>
Use the static in a function method.
This guarantees that it is created and destroyed only once. It also gives you lazy initialization for free.
This also means I don't need to add the object as a member because I have a static function with a static object inside it?
Exactly, the object lives from first call to program termination, so it cannot be a plain data member.
<b>Note:</b>
Use a Singleton if:
<ul>
<li>If you need to have one and only one object of a type in system</li>
</ul>
Do not use a Singleton if:
<ul>
<li>If you want to save memory</li>
<li>If you want to try something new</li>
<li>If you want to show off how much you know</li>
<li>Because everyone else is doing it (See cargo cult programmer in wikipedia)</li>
<li>In user interface widgets</li>
<li>It is supposed to be a cache</li>
<li>In strings</li>
<li>In Sessions</li>
<li>I can go all day long</li>
</ul>
How to create the best singleton:
<ul>
<li>The smaller, the better. I am a minimalist</li>
<li>Make sure it is thread safe</li>
<li>Make sure it is never null</li>
<li>Make sure it is created only once</li>
<li>Lazy or system initialization? Up to your requirements</li>
<li>Sometimes the OS or the JVM creates singletons for you (e.g. in Java every class definition is a singleton)</li>
<li>Provide a destructor or somehow figure out how to dispose resources</li>
<li>Use little memory</li>
</ul>
If you need one and only one object, you create one and only one.<br>
If there is no logical way that two instances could ever be accommodated without irreversibly corrupting the application, you should consider making it a singleton.<br>
And then there's the other aspect, global access: If you don't need global access to the instance, it shouldn't be a singleton.
</p>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-25734333539190264162014-09-11T08:51:00.000+05:302014-09-11T08:51:13.767+05:30C++ Object Slicing"Slicing" is where you assign an object of a derived class to an instance of a base class, thereby losing part of the information - some of it is "sliced" away.
For example,
<pre class="brush:cpp">
class A {
int foo;
};
class B : public A {
int bar;
};
</pre>
So an object of type B has two data members, foo and bar
Then if you were to write this:
<pre class="brush:cpp">
B b;
A a = b;
</pre>
Then the information in b about member bar is lost in a.
Situations where problems would arise
If You have a base class A and a derived class B, then You can do the following.
<pre class="brush:cpp">
void wantAnA(A myA)
{
// work with myA
}
B derived;
// work with the object "derived"
wantAnA(derived);
</pre>
Now the method wantAnA needs a copy of derived. However, the object derived cannot be copied completely, as the class B could invent additional member variables which are not in its base class A.
Therefore, to call wantAnA, the compiler will "slice off" all additional members of the derived class. The result might be an object you did not want to create, because
it may be incomplete,
it behaves like an A-object (all special behaviour of the class B is lost).
staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-7808050520912004842014-09-11T08:02:00.000+05:302014-10-13T12:40:04.846+05:30C++ Interface Example<pre class="brush:cpp">
#include &lt;iostream>
#include &lt;vector>
using namespace std;
// Animal attribute/property is common between Cow and Dog, hence this is the interface
// that contains pure virtual functions of the property that is common to Cow and Dog
// Since this is pure abstract class (since it has a pure virtual function), this class
// cannot be instantiated.
class Animal
{
public:
virtual void Speak() const = 0;
};
// Note:
// You can have an implementation of a pure virtual function.
// A derived class can explicitly call the base class implementation (if access permissions are allow it)
// by using a fully-scoped name (by calling Animal::Speak(), if Animal::Speak() is public or protected (i.e. not private).
// The use case can be when there's a more-or-less reasonable default behavior, but the class
// designed wants that sort-of-default behavior to be invoked only explicitly.
// It can also be the case what you want derived classes to always perform their own work but also be able to call a common set of functionality.
// The other use could be for the case when you want to create a pure abstract class, but it does not have a pure virtual function.
// In that case you can make the destructor a pure abstract method and create the implementation for the destructor.
// Note that even though it's permitted by the language, it's not something that I see commonly used
// (and the fact that it can be done seems to surprise most C++ programmers, even experienced ones).
void Animal::Speak() const
{
cout << "Make some noise" << endl;
}
class Cow : public Animal
{
public:
void Speak() const;
};
void Cow::Speak() const
{
cout << "Moo" << endl;
}
class Dog : public Animal
{
public:
void Speak() const;
};
void Dog::Speak() const
{
cout << "Woof" << endl;
}
void Speak(const Animal& animal) // 1. Since we are passing a const reference, this can only call a const member function.
{ // So we will have to make Animal::Speak() const and in turn Cow::Speak() and Dog::Speak() const
animal.Speak(); // 2. Passing a non const reference will let you modify Animal
} // 3. Passing by value will lead to object slicing
int main(int argc, char *argv[])
{
Cow myCow;
Dog myDog;
vector&lt;Animal*> animalVec; // You can't instantiate abstract classes, thus a vector of abstract classes can't work.
// You can however use a vector of pointers to abstract classes.
// Also storing by value i.e. vector&lt;Animal> animalVec; will lead to object slicing
animalVec.push_back(&myCow);
animalVec.push_back(&myDog);
for(vector&lt;Animal*>::iterator iter = animalVec.begin(); iter != animalVec.end(); ++iter)
{
Speak(**iter);
}
system("PAUSE");
return EXIT_SUCCESS;
}
</pre>
<b>Output:</b></br>
Moo</br>
Woof</br>
Press any key to continue . . .</br>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-6435889840687769922014-09-10T15:10:00.000+05:302014-09-10T15:10:38.966+05:30C/C++ Array<style>
th
{
width:30%;
}
</style>
<table>
<tr>
<th>One dimensional array</th>
<td><pre class="brush:cpp">
int foo [5]; // Array contains integers
</pre>
Initialize:
foo [5] = {1, 2, 3, 4, 5};
foo [5] = {1, 2, 3,}; // 3rd and 4th element are assigned zero
foo [5] = {}; // All elements are assigned zero
foo [] = {1, 2, 3, 4, 5};
Function:
void procedure (int arg[]);
Pass by value:
Pass by reference:
procedure (foo);
Pass by pointer:
</td>
</tr>
<tr>
<th>Two dimensional array</th>
<td><pre class="brush:cpp">
int foo [2][3]; // [row x col] Array contains integers
// Total row*col elements
</pre>
Initialize:
foo [2][3] = {{1, 2, 3}, {4, 5, 6}};
foo [2][3] = {1, 2, 3, 4, 5, 6};
foo [][] = {{1, 2, 3}, {4, 5, 6}};
foo [][] = {1, 2, 3, 4, 5, 6};
foo [2][3] = {{1, 2, 3}}; // All rest elements are assigned zero
foo [2][3] = {}; // All elements are assigned zero
</td>
</tr>
</table>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-40504456762953406782014-09-10T14:13:00.000+05:302014-09-10T14:21:19.140+05:30C/C++ Storage Classes<style>
th
{
width:8%;
}
</style>
<table>
<tr>
<th>auto</th>
<td>Default storage class for all local variables. Can only be used within functions, i.e., local variables.</td>
</tr>
<tr>
<th>register</th>
<td>Used to define local variables that should be stored in a register instead of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can't have the unary '&' operator applied to it (as it does not have a memory location). It should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and implementation restrictions.</td>
</tr>
<tr>
<th>static</th>
<td>Instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.
The static modifier may also be applied to global variables. When this is done, it causes that variable's scope to be restricted to the file in which it is declared.
In C++, when static is used on a class data member, it causes only one copy of that member to be shared by all objects of its class.</td>
</tr>
<tr>
<th>extern</th>
<td>Defines a global variable that is visible to ALL object modules. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.</td>
</tr>
<tr>
<th>mutable (C++)</th>
<td>Applies only to C++ class objects. It allows a member of an object to override constness. That is, a mutable member can be modified by a const member function.</td>
</tr>
</table>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-12892818088908827352014-09-08T18:44:00.001+05:302014-09-08T18:44:37.507+05:30C++ #define Vs typedef<table>
<tr>
<th>#define</th>
<th>typedef</th>
</tr>
<tr>
<td>
Is a preprocessor token: the compiler itself will never see it.
</td>
<td>
Is a compiler token: the preprocessor does not care about it.
</td>
</tr>
<td>
Obeys scoping rules just like variables.
</td>
<td>
Stays valid until the end of the file (or until a matching undef).
</td>
</tr>
<td>
Is a preprocessor directive used to define macros or general pattern substitutions. For eg. #define MAX 100, substitutes all occurrences of MAX with 100
</td>
<td>
Creates an "alias" to an existing data type. For e.g. typedef char chr;
</td>
</tr>
</tr>
<td>
<pre class="brush:cpp">
#define INTPTR int*
...
INTPTR a, b;
</pre>
After preprocessing, that line expands to
<pre class="brush:cpp">
int* a, b;
</pre>
Only a will have the type int *; b will be declared a plain int (because the * is associated with the declarator, not the type specifier).
</td>
<td>
<pre class="brush:cpp">
typedef int *INTPTR;
...
INTPTR a, b;
</pre>
In this case, both a and b will have type int *.
</td>
</tr>
</table>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-69973852198866692452014-09-07T19:20:00.000+05:302014-09-08T18:22:38.582+05:30C++ Const keywordYou have to read pointer declarations right-to-left.</br></br>
<table>
<tr>
<td>
<pre class="brush: cpp">
const int x = 10;
</pre>
</td>
<td>x's value cannot be changed</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
int const x = 10;
</pre>
</td>
<td>Same as above</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
static int x = 10;
</pre>
</td>
<td>Same as above</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const int const x = 10;
</pre>
</td>
<td>This is nonsense. Pointers have two different kinds of const qualifiers makes sense, one is for the pointer itself, the other for what the pointer points. But it doesn't make sense for int type to have two different kinds of const qualifiers. Just use one</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
foo const* p;
</pre>
</td>
<td>p points to a constant foo. The foo object can't be changed. It means p points to an object of class foo, but p can't be used to change that foo object (naturally p could also be NULL). For example, if class foo has a const member function called inspect(), saying p->inspect() is OK. But if class foo has a non-const member function called mutate(), saying p->mutate() is an error</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const foo* p
</pre>
</td>
<td>Same as above</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
foo* const p;
</pre>
</td>
<td>p is a const pointer to a foo. You can't change the pointer p, but you can change the foo object</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
foo const* const p;
</pre>
</td>
<td>p is a const pointer to a const foo.You can't change the pointer p itself, neither can you change the foo object</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const foo* const p;
</pre>
</td>
<td></td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
foo const& r;
</pre>
</td>
<td>It means r aliases a foo object, but r can't be used to change that foo object. For example, if class foo has a const member function called inspect(), saying r.inspect() is OK. But if class foo has a non-const member function called mutate(), saying r.mutate() is an error</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const foo& r;
</pre>
</td>
<td>Same as above</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
foo& const r;
</pre>
</td>
<td>This is nonsense. r is a const reference to a foo. But that is redundant, since references are always const. You can't reseat a reference. Never. With or without the const. In other words, "foo& const r" is functionally equivalent to "foo& x". Since you're gaining nothing by adding the const after the &, you shouldn't add it since it will confuse people — the const will make some people think that the foo is const, as if you had said "foo const& x".</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
int GetValue() const;
</pre>
</td>
<td>This member function cannot change this pointer, i.e. the class members of this pointer. It can however change any other</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const foo GetValue();
</pre>
</td>
<td></td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const int& GetValue();
</pre>
</td>
<td>Returns a const reference to the member variable if it's a getter method. This won't make copies and return.</td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
int* GetValue();
</pre>
</td>
<td></td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const int* GetValue();
</pre>
</td>
<td></td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const int GetValue() const;
</pre>
</td>
<td></td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const int& GetValue() const;
</pre>
</td>
<td></td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
int* GetValue() const;
</pre>
</td>
<td></td>
</tr>
<tr>
<td>
<pre class="brush: cpp">
const int* GetValue() const;
</pre>
</td>
<td></td>
</tr>
</table>
</br>
Note: const class objects can only call const member functions
</br></br>
Three ways you can initialize a const member variable.</br>
1. Inside the class declaration</br>
<pre class="brush:cpp">
static const int x = 10;
</pre>
2. Outside the class</br>
<pre class="brush:cpp">
class A
{
static const int a;
};
const int A::a = 10; //defining the static member outside the class
</pre>
Note: static means there is a single copy of the variable for all instances of the object, constant or not. It creates just one copy of it for all the objects. It is a design choice that needs to be considered carefully.</br>
3. In constructor initialization list</br>
<pre class="brush:cpp">
class A
{
const int b;
A(int c):b(c){} //const member initialized in initialization list
};
</pre></br>
Note: "mutable" keyword is used with member variables which we want to be able to change even on const class objects. Hence, mutable data members of const objects can be modified</br>
<pre class="brush:cpp">
class A
{
int x;
mutable int y;
public:
A()
{
x = 0;
y = 0;
}
void func() const
{
i++; // Error, cannot modify
j++; // Works, because y is mutable
}
void func()
{
i++; // Error, cannot modify
j++; // Works, because y is mutable
}
};
int main()
{
const A constObj;
constObj.func(); // Calls the const func method, since constObj is const object
A obj;
obj.func(); // Call the non const func method, since obj is non const object
}
</pre>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-33754014445874036282014-09-07T13:47:00.000+05:302014-09-10T14:23:38.953+05:30C++ Reference Vs Pointer<table>
<tr>
<th>Reference</th>
<th>Pointer</th>
</tr>
<tr>
<td>A reference is a variable that refers to something else and can be used as an alias for that something else. A reference is the object. A reference can be thought of as a constant pointer (not to be confused with a pointer to a constant value!) with automatic indirection, i.e. the compiler will apply the * operator for you. </td>
<td>A pointer is a variable that stores a memory address, for the purpose of acting as an alias to what is stored at that address.</td>
</tr>
<tr>
<td>Const references can be bound to temporaries. This makes const& safer for use in argument lists and so forth.</td>
<td>Pointers cannot (not without some indirection).
<pre class="brush: cpp">
const int &x = int(12); // legal C++
int *y = &int(12); // illegal to dereference
// a temporary.
</pre>
</td>
</tr>
<tr>
<td>A reference cannot be reassigned, and must be assigned at initialization. A reference can not be re-seated/reassigned after binding. A reference, always refers to the object with which it is initialized</td>
<td>A pointer can be re-assigned any number of times.
<pre class="brush: cpp">
int x = 5;
int y = 6;
int *p;
p = &x; // p points to x
p = &y; // p now points to y
*p = 10; // p now is 10
// Another special example
string s1("Nancy");
string s2("Clancy");
string& rs = s1; // rs refers to s1
string *ps = &s1; // ps points to s1
rs = s2; // rs still refers to s1,
// but s1's value is now "Clancy",
// as if s1 = s2
ps = &s2; // ps now points to s2;
// s1 is unchanged
</pre>
</td>
</tr>
<tr>
<td>References always refer to an object. Because a reference must refer to an object, C++ requires that references be initialized
<pre class="brush: cpp">
string& rs; // error! References must
// be initialized
string s("xyzzy");
string& rs = s; // okay, rs refers to s
int &r = NULL; //<--- compiling error
</pre>
</td>
<td>Pointers can point to nowhere (NULL).
<pre class="brush: cpp">
string *ps; // uninitialized pointer
// Its valid but risky
int *p = NULL;
</pre>
</td>
</tr>
<tr>
<td>The fact that there is no such thing as a null reference implies that it can be more efficient to use references than to use pointers. That's because there's no need to test the validity of a reference before using it
<pre class="brush: cpp">
void printDouble(const double& rd)
{
cout << rd; // no need to test rd; it
}
</pre>
</td>
<td>Pointers, on the other hand, should generally be tested against null.
<pre class="brush: cpp">
void printDouble(const double *pd)
{
if (pd) // check for null pointer
{
cout << *pd;
}
}
</pre>
</td>
</tr>
<tr>
<td>You can't test if a reference is NULL.
<pre class="brush: cpp">
if(&ref == NULL) // Not possible
</pre>
</td>
<td>You can test if a pointer is NULL.
<pre class="brush: cpp">
if(*ptr == NULL) // Possible
</pre>
</td>
</tr>
<tr>
<td>References only offer one level of indirection. </td>
<td>You can have pointers to pointers to pointers offering extra levels of indirection. </td>
</tr>
<tr>
<td>You can't take the address of a reference.</td>
<td>You can take the address of a pointer.</td>
</tr>
<tr>
<td>There's no "reference arithmetics" (but you can take the address of an object pointed by a reference and do pointer arithmetics on it e.g. &obj + 5).</td>
<td>You can perform arithmetics with pointers, like post increment, pre increment. Pointers can iterate over an array, you can use ++ to go to the next item that a pointer is pointing to, and + 4 to go to the 5th element. This is no matter what size the object is that the pointer points to.</td>
</tr>
<tr>
<td>Regardless of how a reference is implemented, a reference has the same memory address as the item it references.</td>
<td>A pointer is a variable that holds a memory address.</td>
</tr>
<tr>
<td>References cannot be stuffed into an array.</td>
<td>Pointers can be stuffed into an array.</td>
</tr>
<tr>
<td>A reference can be used directly.</td>
<td>A pointer needs to be dereferenced with * to access the memory location it points to.</td>
</tr>
<tr>
<td>A reference to a class/struct uses . to access its members.</td>
<td>A pointer to a class/struct uses -> to access its members.</td>
</tr>
<tr>
<td>There is one other situation in which you should use a reference, and that's when you're implementing certain operators. The most common example is operator[]. This operator typically needs to return something than can be used as the target of an assignment.
<pre class="brush: cpp">
vector &lt;int> v(10); // create an int vector of size 10;
v[5] = 10; // the target of this assignment
// is the return value of operator[]
</pre>
If operator[] returned a pointer, this last statement would have to be written this way:
<pre class="brush: cpp">
*v[5] = 10;
</pre>
But this makes it look like v is a vector of pointers, which it's not. For this reason, you'll almost always want operator[] to return a reference.
</td>
<td>&nbsp;</td>
</tr>
</table>
</br>
As a general rule,</br>
Use references in function parameters and return types to define useful and self-documenting interfaces.</br>
Use pointers to implement algorithms and data structures.staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-79139974650173788962014-08-27T19:45:00.000+05:302014-09-10T14:26:41.150+05:30C++ Abstract and Interface fundamentals<style>
th
{
width:15%;
}
</style>
<table>
<tr>
<th>Virtual Function</th>
<td>A function preceded by virtual keyword. Intention is that this function would be overloaded in some derived classes. All virtual functions need not be overridden in some derived classes. They can remain in base class as it is.
<pre class="brush:cpp">
virtual void VirtualFunction();
</pre>
</td>
</tr>
<tr>
<th>Pure Virtual Function</th>
<td>A virtual function with no implementation which is set equal to zero.
<pre class="brush:cpp">
virtual void PureVirtualFunction() = 0;
</pre>
This can have an implementation in base class. You cannot instantiate a class having a pure virtual function. If you create a pointer of base class pointing to the derived class, it would call the function of the derived class. So, how do you use it?
<pre class="brush:cpp">
DerivedClass myDerivedClass;
BaseClass* myBaseClass;
myBaseClass = &myDerivedClass; // In one line BaseClass* myBaseClass = &myDerivedClass;
myBaseClass->PureVirtualFunction(); // This would call implementation of the pure virtual
// function in derived class
</pre>
You have to BOTH override AND implement a Pure Virtual Function in the class that derived from the class that had the pure virtual function. If no class derived from it, then it's not required.
</td>
</tr>
<tr>
<th>Abstract Function</th>
<td>Synonym for Pure Virtual Function. A function that is pure virtual.
<pre class="brush:cpp">
void AbstractFunction() = 0;
</pre>
</td>
</tr>
<tr>
<th>Abstract Class</th>
<td>Class that has at least one Pure Virtual Function</td>
</tr>
<tr>
<th>Interface Class</th>
<td>Class that has all functions Pure Virtual. And it has no member variables. This is not a rule per se. Intention is that the interface class will not have any implementation of its own. All required concrete class that derive from it must implement/override the functions in the interface. Note that there is a great temptation to add concrete member functions and data to iterface classes. This must be resisted, in general it is a sign that the interface is not well factored. Data and concrete member functions tend to imply a particular implementation and as such can inherit from the interface but should not be in that interface. A general test is the "is a" vs "has a", as in a Square is a Rectangle, but a Square has a set of sides. In other words, "is a" would use Abstract class and "has a" would use Interface.
</td>
</tr>
<tr>
</table>
</br>
Note: The ISO C++ Standard specifies that all virtual methods of a class that are not pure-virtual must be defined. Note that a destructor must be defined even if it is declared pure-virtual.
Ref: http://gcc.gnu.org/faq.html#vtables
</br></br>
<b>Synonyms:</b></br>
Base Class/Super Class/Parent Class</br>
Derived Class/Inherited Class/Sub Class</br>
Abstract Function/Pure Virtual Function</br>
Instantiate/Create</br>
Concrete/Independent</br>
Abstraction</br>
Indirection</br>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-90949172784523263482014-08-24T12:36:00.002+05:302014-08-24T12:40:43.346+05:30Convert FLV to MP4 or MKV without any loss<div dir="ltr" style="text-align: left;" trbidi="on">
<pre>
Steps:
1. Demux the FLV using FLV Extract
http://www.moitah.net/ or https://code.google.com/p/flv-extraxct/
FLVExtractCL -v -a INPUT.flv
2. Remux the video.264 and audio.aac to MP4 or MKV
MP4Box -add videoFile.h264 -add audioFile.aac OUTPUT.mp4
mkvmerge -o OUTPUT.mkv videoFile.264 audioFile.aac
ffmpeg -i INPUT.flv -vcodec copy -acodec copy OUTPUT.mp4
Remember you need to set the correct framerate when muxing h264 video.
You can load the mp4 from ffmpeg directly into mkvmerge but I had a few issues so I just demux and then mux into mkv
and I don't have any problems
Use MP4Box to extract raw streams from mp4 files
MP4Box -raw 1 -raw 2 INPUT.mp4
MP4Box -raw 1:output=filename INPUT.mp4
MP4Box -raw * INPUT.mp4
for users who dont like to code or type, a simple GUI for lossless packaging of RAW audio data (or RAW video data) is yamb.
I even prefer that to fixing FLV's with FLV Fix tools.
For example, when you (losslessly) extract *.264 and *.aac with FLVExtract and you then try to package them as *.mp4 with yamb,
then use the "Avg Frame Rate" (and not the "True Frame Rate") displayed by FLVExtract. yamb sometimes ask you to enter "frame
rate", sometimes it doesn't.
If the MP4 is still out of sync (audio vs. video), then use as "frame rate" the "videoframerate" as seen by "FLV Fix" (Replay
Media Catcher 4) after you have fixed the FLV with "Fix Duration".
</pre>
</div>
staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-23784959067564072642014-08-12T18:05:00.002+05:302014-09-07T10:11:31.569+05:30Function: Parameter as Reference or Pointer<div dir="ltr" style="text-align: left;" trbidi="on">
<pre class="brush:cpp">
void funcParameterAsReference(int& inputOrOutput)
{
inputOutputVal = 5;
}
int myInt = 0;
funcReference(myInt);
</pre>
OR
<pre class="brush:cpp">
void funcParameterAsPointer(int* inputOrOutput)
{
*inputOutputVal = 5;
}
int myInt = 0;
funcPointer(&myInt);
</pre>
My rule of thumb is:
</br>
Use pointers if you want to do arithmetic with them or if you ever have to pass a NULL-pointer.
Use references otherwise. So for input parameter use const reference and for output parameter use plain reference.
</br>
As input parameter :
<pre class="brush:cpp">
void func(const int& input);
</pre>
As output parameter :
<pre class="brush:cpp">
void func(int& Output);
</pre>
Why should pass-by-reference be used instead of pass-by-pointer? The most obvious reason is that a reference cannot be null.
In a function that takes a pointer, you have to check that the pointer is not null before you use it, at least with a debug assertion. During a proper code review you have to analyze more code to be sure that you don't accidentally pass a null pointer to a function that doesn't expect one. I've found that it takes much longer to review functions that take pointer arguments for this very reason; it's so much easier to get it wrong when using pointers.
</br>
One advantage of using pointer as a parameter is when you have to add a new parameter to an existing function (for whatever reason). This way you can pass NULL to it, if it doesn't use the parameter in the implementation (definition)
<pre class="brush:cpp">
void myFunc1(int* inp);
void myFunc2(int* inp);
int main()
{
int inp = 0;
myFunc1(&inp); // You can't pass NULL to myFunc1 because it uses inp in the implementation. If NULL is passed,
// it will crash.
// e.g.
// int* inp = NULL;
// myFunc1(inp);
// OR
// myFunc1(NULL);
cout << "inp = " << inp << endl;
myFunc2(NULL); // You can pass NULL to myFunc2 because it doesn't use inp in the implementation
system("PAUSE");
return 0;
}
void myFunc1(int* inp)
{
*inp = 99;
cout << "I am in myFunc1" << endl;
}
void myFunc2(int* inp)
{
cout << "I am in myFunc2" << endl;
}
</pre>
Ref: <a href="http://stackoverflow.com/questions/4028413/out-parameters-and-pass-by-reference">http://stackoverflow.com/questions/4028413/out-parameters-and-pass-by-reference</a>
<br /></div>
staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-61865862964244004522014-05-05T08:29:00.000+05:302014-05-05T08:29:09.842+05:30PATHEXT environment variablePATHEXT environment variable contains file extensions.
This means for these file extensions, user doesn't have to type in the file extension to run the exe in a command prompt.
staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-8196480279521789262014-05-05T08:23:00.000+05:302014-05-05T08:23:06.364+05:30Batch file with same filename as the first command in itIf batch file has the same filename as the first command in it, running it causes it to run in a infinite loop.
E.g. the batch file is named "ipconfig.bat"
and it has
ipconfig /flushdns
What happens is when it hits the first command i.e. ipconfig /flushdns then it simply reenters the batch file.
For example onsider if you have a batch file called "find.bat"
echo this is find.bat. how are things.
find /i %1 %2
What happens here?
find .bat calls itself on the second line, because windows searches in the current directory first - the location of your batch file; for any file that has an extension defined in "PATHEXT" environment variable; defaults are exe,com,pif,vbs,js, and bat.
So the find.bat that is currently executed is run instead of the intended find.exe that is in the system folder.
So to avoid this problem, use any of the two approaches below
1. Rename the batch file to something different than the commands inside it.
2. Give explicit path to the command's exe for the commands in the batch file. E.g C:\Windows\System32\ipconfig /flushdns
staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-2872813669863623692014-02-22T14:45:00.000+05:302014-02-22T15:57:51.946+05:30Sign In option doesn't appear in blogspot.com in Firefox<span style="background-color: #E0EAF1">Sign In</span> option is available on the <font style="background-color: #E0EAF1">Navbar</font> on the top right corner. <br />
<br />
<img src="http://imageshack.com/a/img30/3967/8s26.png"><br />
<br />
Recently this appears to have disappeared when opened in Firefox.<br />
This is a new issue and seems to occur because of some javascript issue and a filter rule in Fanboy's Social Blocking List in Adblock Plus.<br />
The Filter rule causing this is <span style="background-color: #E0EAF1">||google.com/js/plusone.js$third-party</span>. You need to disable this filter.<br />
In Firefox, goto <font style="background-color: #E0EAF1">Tools &rarr; Adblock Plus &rarr; Filter preferences</font><br />
Under <span style="background-color: #E0EAF1">Filter subscriptions</span> tab, select <span style="background-color: #E0EAF1">Fanboy's Social Blocking List</span>.<br />
<br />
On the right side there are the filters. If you do not see the filters on right, hover your mouse on the dotted line on the right till a <span style="background-color: #E0EAF1">Hand</span> appears, then click on it.<br />
<br />
<img src="http://imageshack.com/a/img22/1251/q6i5.png"><br />
<br />
Scroll down to <span style="background-color: #E0EAF1">||google.com/js/plusone.js$third-party</span> and uncheck it.<br />
<br />
<img src="http://imageshack.com/a/img841/7420/om39.png"><br />
<br />
<img src="http://imageshack.com/a/img198/6991/5gbw.png"><br />
<br />
Close <span style="background-color: #E0EAF1">Adblock Plus Filter preferences</span>.<br />
Now open blogspot webpage or refresh an existing blogspot webpage. The <font style="background-color: #E0EAF1">Navbar</font> should be back with the <span style="background-color: #E0EAF1">Sign In</span> option.staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-3202869325455382014-02-21T19:41:00.000+05:302014-09-14T14:52:22.942+05:30C++ std::map<p>
Maps are associative containers that store elements formed by a combination of a key value and a mapped value, following a specific order. map&lt;<i>Key, Value</i>><br>
<b>Properties of maps</b><ol>
<li>Like Set, Map (ordered or unordered) does not allow duplicate <b><i>Key</i></b></li>
<li>Ordered map is sorted by it's <b><i>Key</i></b>. It is sorted following a specific strict weak ordering criterion indicated by its internal comparison object <b>std::map::key_comp</b>. So a map of user defined class objects which have no meaning of comparison, will not be sorted.</li>
<li>Unordered map is <b>not</b> sorted by <b><i>Key</i></b></li>
<li>map is generally slower than unordered_map to access individual elements by their key <b><i>map[Key]</i></b>. So if std::find is mostly what you will be doing, unordered_map is better choice.</li>
<li>unordered_map is generally less efficient for range iteration through a subset of it's elements than ordered map. So if iteration is mostly what you will be doing, then ordered map is a better choice.</li>
<li>Maps are typically implemented as binary search trees.</li>
<li>A unique feature of map among associative containers is that they implement the direct access operator (operator[]), which allows for direct access of the mapped value.</li>
</ol>
<b>Proof of key uniqueness and sorting properties</b>
<pre class="brush:cpp">
cout << "Ordered Map" << endl;
cout << "===============================" << endl;
map&lt;int, string> myMap;
myMap.insert(make_pair(33, "Hello"));
myMap.insert(make_pair(11, "Cello"));
myMap.insert(make_pair(11, "Vello"));
myMap.insert(make_pair(44, "Jello"));
myMap.insert(make_pair(22, "Fello"));
for(map&lt;int, string>;::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
{
cout << "&lt;Key, Value> = &lt;" << iter->;first << ", " << iter->;second << ">" << endl;
}
cout << endl;
cout << "Unordered Map" << endl;
cout << "===============================" << endl;
unordered_map&lt;int, string> myUnorderedMap;
myUnorderedMap.insert(make_pair(33, "Hello"));
myUnorderedMap.insert(make_pair(11, "Cello"));
myUnorderedMap.insert(make_pair(11, "Vello"));
myUnorderedMap.insert(make_pair(44, "Jello"));
myUnorderedMap.insert(make_pair(22, "Fello"));
for(unordered_map&lt;int, string>::iterator iter = myUnorderedMap.begin(); iter != myUnorderedMap.end(); ++iter)
{
cout << "&lt;Key, Value> = &lt;" << iter->first << ", " << iter->second << ">" << endl;
}
</pre>
<u><b>Output</b></u>
<pre>
Ordered Map
===============================
&lt;Key, Value> = <11, Cello>
&lt;Key, Value> = <22, Fello>
&lt;Key, Value> = <33, Hello>
&lt;Key, Value> = <44, Jello>
Unordered Map
===============================
&lt;Key, Value> = <33, Hello>
&lt;Key, Value> = <11, Cello>
&lt;Key, Value> = <44, Jello>
&lt;Key, Value> = <22, Fello>
</pre>
<b>Find in a map by <i>Key</i></b><br>
<pre class="brush:cpp">
if(myMap.find("f") != myMap.end())
{
// Found
}
else
{
// Not found
}
</pre>
<b>Find in a map by <i>Value</i></b><br>
There are four options
<ol>
<li>Use boost::bimap if boost library is available</li>
<li>Create another map with Value as the Key and use
map::find on it</li>
<li>Use std::find_if with a predicate/functor in which you
define some equal to comparison logic</li>
<li>
Iterate through the whole map to find the value
<pre class="brush:cpp">
for(map&lt;int, string>::iterator iter = myMap.begin(); iter != myMap.end(); ++iter)
{
if(iter->second == "Hello")
{
// Found
break;
}
}
</pre>
</li>
</ol>Option 1 & 2 are equivalent. Runtime will be O(log n) for a map and O(1) for an unordered map. Option 2 you are essentially reinventing the boost:bimap. Option 3 &amp; 4 are equivalent. Runtime in both cases is O(n). They both iterate the whole map entry-by-entry until a matching entry is found. Hence, if boost library is not available, go for option 2. Create two maps and implement functionality to keep them synchronized.
<br><br>
<b>Free memory from a map of object pointers</b>
<ul>
<li>map or any STL container destroys it's elements while being destroyed itself. If the map had stored some objects, those objects will be destroyed. If it had stored pointers, those pointers will be destroyed, but not the actual objects pointed to by the pointers. So, you have to destroy those instances by yourself explicitly, iterate through std::map elements and call delete on contained pointer.</li>
<li>std::map::clear() empties out the contents of the map, i.e. all contained pointers will be destroyed. It doesn't do anything with the objects pointed by them, in particular it doesn't delete them. All those objects will stay alive in memory.</li>
</ul><b>Note:</b>
Raw pointers do not have ownership of the objects they point to. So when a pointer is destroyed, it will not delete whatever
it points to. Generally speaking, when you have a pointer, there is no way to know if:
<ul>
<li>It points to a valid object at all (you don't want to call delete on some random garbage in memory),</li>
<li>The object it points to has been allocated with new (if it has been allocated in another way, it should not be deleted with delete), or</li>
<li>If there are other pointers that also point to the same object (in which case only one of them should call delete. Which one?)</li>
</ul>
So even if you wanted to, there is no way to automatically delete an object when a pointer to it is destroyed.
</p>
staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0tag:blogger.com,1999:blog-2501331280977423982.post-35131725884522351702014-02-09T16:03:00.001+05:302014-02-09T16:06:06.285+05:30C++ Functor Predicate with std::find_if<div dir="ltr" style="text-align: left;" trbidi="on"><pre>struct StringFinder
{
StringFinder(const std::string & st) : s(st) { }
const std::string s;
bool operator()(const RegPair& lhs) const { return lhs.first == s; }
}
std::find_if(sequence.begin(), sequence.end(), StringFinder(foo));
</pre><br />
<pre>That's not how predicates work. You have to supply either a free function bool Comparator(const MyClass & m) { ... }, or build a function object, a class that overloads operator():
struct MyClassComp
{
explicit MyClassComp(int i) n(i) { }
inline bool operator()(const MyClass & m) const { return m.myInt == n; }
private:
int n;
};
std::find_if(v.begin(), v.end(), MyClassComp(5));
</pre><br />
<pre>A Functor is a object which acts like a function. Basically, a class which defines operator().
class MyFunctor
{
public:
int operator()(int x) { return x * 2;}
}
MyFunctor doubler;
int x = doubler(5);
The real advantage is that a functor can hold state.
class Matcher
{
int target;
public:
Matcher(int m) : target(m) {}
int operator()(int x) { return x == target;}
}
Matcher Is5(5);
if (Is5(n)) // same as if (n == 5)
{ ....}
</pre><br />
<pre>a functor is an object that acts like a function, i.e. it overloads the function call operator.
Functors are commonly used in STL algorithms. They are useful because they can hold state before and between function calls, like a closure in functional languages. For example, you could define a MultiplyBy functor that multiplies it's argument by a specified amount:
class MultiplyBy {
private:
int factor;
public:
MultiplyBy(int x) : factor(x) {
}
int operator () (int other) const {
return factor * other;
}
};
Then you could pass a MultiplyBy object to an algorithm like std::transform:
int array[5] = {1, 2, 3, 4, 5};
std::transform(array, array + 5, array, MultiplyBy(3));
// Now, array is {3, 6, 9, 12, 15}
Another advantage of a functor over a pointer to a function is that the call can be inlined in more cases. If you passed a function pointer to transform, unless that call got inlined and the compiler knows that you always pass the same function to it, it can't inline the call through the pointer.
</pre><br />
<pre>You can do it with a functor or a regular function that is not part of MyClass, or with a static function inside MyClass - here's an example with non-member function (basically just removing the MyClass:: part of the condition definition):
#include <algorithm>
#include <vector>
using namespace std;
class Foo
{
//whatever
};
class MyClass
{
public:
int myInt;
vector<foo> foo_v;
};
bool condition(MyClass mc)
{
if(mc.myInt==5)
return true;
else
return false;
}
int main (void)
{
vector<myclass> myClass_v;
std::find_if(myClass_v.begin(),myClass_v.end(),condition);
}
</pre><br />
</div>staraphdhttp://www.blogger.com/profile/09503490206611374487noreply@blogger.com0