Perhaps a custom sort on the vector might solve your problem. See attached link for ideas
http://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects
Perhaps a custom sort on the vector might solve your problem. See attached link for ideas
http://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects
C++ does name mangling to ensure method/function names are unique, C does not. So when calling C function from C++ you need to let C++ know it’s a C function. This can be done using the extern "C", read below for examples
extern "C" {
#include "header.h"
}
google c++ const
An alternative is to have the push return true/false...
template<int MAX_SIZE>
class Stack
{
public:
Stack()
:_count(0)
{
}
bool push(int val)
{
if( _count < MAX_SIZE )
{
_array[_count++] = val;
return true;
}
return false;
}
private:
int _count;
int _array[MAX_SIZE];
};
A structure is a class where members are public by default, besides that technically speaking there are same. However by convention, structures are used to represent simple data structures and classes for more expressive functionality.
For this simple application, it’s not a big deal, but meticulous attention to Constructors and Destructors, including the initialization and order of initialization will save you hours of debugging time in the future.
In c/c++ its a good idea to initialize variables to some known state. Unintialized variables causing problem has kept many a programmer debugging for hours.
In C++ Null is defined as 0. See section on "Should I use NULL or 0?" for some words of wisdom from the master himself.
template <typename Tdata>
void q1_sort(vector<Tdata> &A, int left, int right)
{
if((right-left) < ISORT_CUTOFF){
if(data_t == 1)
{in_sort<Tdata>(A, (int)left, (int)right+1);}
else if(data_t == 2)
{in_sort<Tdata>(A, (int)left, (int)right+1);}
}
else{
q1_sort<Tdata>(A, left, i-1);
q1_sort<Tdata>(A, i+1, right);
}
}
1. use typedefs to simlify your code
2. use strings rather than char* for key in containers.
3. if you must use char *, custom comparison perdicate must be provided
Below is simple sample
#include <iostream>
#include <string>
#include <map>
int main()
{
typedef std::map<std::string,std::string> TMap;
TMap m;
m["k2"] = "v2";
m["k3"] = "v3";
m["k1"] = "v1";
TMap::iterator iter;
for(iter=m.begin(); iter != m.end(); ++iter)
{
std::cout << "key=" << iter->first << " value=" << iter->second << std::endl;
}
return 0;
}
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::stringstream expression(" 3 + 4 ( 5 * 8 ");
std::string token;
const char DELIMITER(' ');
while (std::getline(expression, token, DELIMITER))
{
std::cout << token << std::endl;
}
}
1. Ofcourse newbies can learn C++!!!
2. Not a game programmer, but I suspect lots of games use c++ since performance is important
3. I use a mac at home, linux/windows at work. You already have everything you need.
a. open a terminal window on your mac
b. type nano (or other editor you know, I use vi since its on every unix system)
c. write your first c++ program:
#include <iostream>
int main()
{
std::cout << "I can do it!!!" << std::endl;
}
d. build program with g++ compiler: $ g++ -g hello.cpp -o hello
e. run program on bash shell: $ ./hello
f. debug program with gdb debugger: $gdb hello
Now go read up on everything, Enjoy and Have Fun!!!
Abstract Data Types is a way to generalize/model software so that the same interface used with different implementations and types.
Standard Template Library is a good example of Abstract Data Types. For example the interface for stack is independent of the implementation. Under the hood stack may be implemented by linked list, array or whatever is best suited for the problem.
In addition using templates same software model can be used with different data type; floats int, pointer to people objects etc. Below is an example of std::stack used with int and float.
#include <iostream>
#include <stack>
int main( int argc, char *argv[])
{
// creat int stack
std::stack<int> i;
i.push(1); i.push(3); i.push(5);
// create float stack
std::stack<float> f;
f.push(2.9884); f.push(4.8885); f.push(6.444);
// show int stack
while( !i.empty() )
{
std::cout << i.top() << std::endl;
i.pop();
}
// show float stack
while( !f.empty() )
{
std::cout << f.top() << std::endl;
f.pop();
}
return 0;
}
A problem unique to multi-core is that each core has its own cache and caches between cores must be synchronized if different threads are accessing the same variable.
So you may get into a situation where a shared resource has different values in the different cache, i.e. the caches did not synchronize. For example this may happen if a shared resource is not declared volatile.
Comparing floating point numbers using == or != is not safe. The difference between the two numbers must be compared to some tolerance, usually the system epsilon. (see sample code below).
bool isEqual( float v1, float v2 )
{
if( std::abs(v1-v2) < std::numeric_limits<float>::epsilon() )
{
return true;
}
return false;
}