I usually use std::getline, and yes it's true it returns the istream object.
I commmonly do this:
string inputLine;
while( getline(inFile,inputLine) )
{
//... Process Line
}
I usually use std::getline, and yes it's true it returns the istream object.
I commmonly do this:
string inputLine;
while( getline(inFile,inputLine) )
{
//... Process Line
}
There is most likely a warning level you can set to have the compiler only display more critical warnings.
I'm not a 3D expert, but I would find an accurate timer, and use it. For instance, on Windows you could use this: http://msdn.microsoft.com/en-us/library/ms724408%28v=vs.85%29.aspx
or on this page, the section "The following functions are used with high-resolution performance counters."
http://msdn.microsoft.com/en-us/library/ms725473%28v=VS.85%29.aspx
(get it? no?.. nvm)
Yes.
If you're assigning a pointer the address of a stack allocated variable you need worry about when that variable goes out of scope, it will be destroyed! Then your pointer doesn't point to anything, and you will get some bad seg-fault type error messages at run time (meaning the program crashes).
With dynamic allocation it is common to leak memory, which when done to excess can cause your application to crash also. I only don't recommend dynamic allocation because you must keep track of the pointer and properly delete it, and with your current ideas a smart pointer is totally superior for that.
But in theory/practice you could copy an array of pointers to stack allocated objects and use them etc. As long as you make sure those variables don't go out of scope...
Or you could dynamically allocate and delete them.
smart pointers automatically delete the dynamically allocated variable when it goes out of scope.
The standard library has a smart pointer type, it's the std::auto_ptr<> BUT you can't put those in the STL containers for some lame reason. Thus, many opt to use the boost library's boost::shared_ptr<>.
I feel it is also important to note that programs often have limited stack space--with a limit of around 1 MB being common (as far as I know). Dynamically allocating memory uses the "heap" instead of the "stack" which will give you a much greater amount of memory …
Probably some input left in the stream. There are tutorials for avoiding this. Which is probably the best idea if you want to know what's going on.
Otherwise you can use my method:
int someInt = 0;
cin >> someInt; //<- if the user bunks this up and
//enters say a letter, cin will enter fail state, and there
//will probably be data left in the buffer--which happens in other
//ways also.
//so:
cin.sync();//<-- ignore anything left in the stream.
cin.clear();//<-- clear error bits.
See here for more info: http://www.cplusplus.com/reference/iostream/istream/
make sure your braces match up. { }
I'm assuming the pointers point to dynamically allocated data, probably allocated with "new" ?
In that case, it is very important you do not lose track of the pointers that point to data allocated with "new". When you copy a pointer to the vector that has pointers in it, if you're over-writing a pointer pointing to data you may lose the only pointer to the location of the data in memory, and thus will leak memory.
It is my opinion that the proper way to do it is actually to use a smart pointer type, such as those provided by the boost lib. See boost::shared_ptr<>
Otherwise, if you must use raw pointers, delete the pointer you are going to over-write, set it to nullptr, then copy the new pointer over. If you wish the pointer to still be useful, do NOT call delete on the other copy of hte pointer in the other vector.
Instead, set it to nullptr.
Ex:
for(int i=0; i<m_vPopulation2.size(); i++)
{
delete m_vPopulation2[i];
m_vPopulation2[i] = m_vParentPop2[i];
m_vParentPop2[i] = nullptr;//or NULL (0x0)
}
http://www.cplusplus.com/reference/stl/set/ //container info
http://www.cplusplus.com/reference/std/iterator/BidirectionalIterator/ //iterator type info
ex:
for( set<char>::iterator it = myset.begin(); it != myset.end(); ++it){
//use iterator
}
If your compiler has C++0x TR1 support, you can use the auto keyword for the type, otherwise you can use the iterator type.
I'm not exactly sure what you want, but here is an example of iterating over a set with iterators.
If iterators are new to you, you may wish to read about them before jumping in to using the STL.
set< int > myset;
myset.insert(100);
myset.insert(200);
myset.insert(300);
for( set<int>::iterator it = myset.begin(); it != myset.end(); ++it)
{
//Use iterator.
cout << (*it) << endl;
}
Yes it does waste at most (N/2)-1 units of memory, I'm not an expert at memory management, I'm a college student--that's just something I picked up on reading a random website a year or two ago. The website was basically some guy ranting about how crappy his memory management was, then the perfect solution was to allocate N*2--like it was a magical formula made by god. Kind of funny, actually.
and yes you do delete it, my bad.
You could try implementing the sieve of eratosthenes using a vector<bool> or a bitset, the wikipedia page on the sieve is decent; the vector<bool> is for marking it as not prime.
The indices would be the numbers.
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
You also have other options, of course. It is my belief that Era's sieve will be the fastest.
http://en.wikipedia.org/wiki/Sieve_of_Sundaram
Here, this according to wikipedia, is a modern version of Eratosthenes' sieve:
http://en.wikipedia.org/wiki/Sieve_of_Atkin
see also: http://en.wikipedia.org/wiki/Wheel_factorization
And, it's up to you as the master and commander of your project to determine the one that will suit your needs of both difficulty in implementation/time used and the required performance.
void Add(DataType p_item)
{
int m_newSize = m_size+1;
DataType* tempArr = new DataType[m_newSize];
for(int i=0;i<m_size;i++)
tempArr[i] = m_array[i];
tempArr[m_size] = p_item;
m_size = m_newSize;
if(m_array!=0)
delete[] m_array;
m_array = tempArr;
tempArr = 0;
}
Well there are some serious problems with this. I expected your class to be something similar to a vector, meaning it will allocate a large block of memory and then allow access to a certain part of it. Reallocation can be time-consuming and the best way to avoid frequent re-allocation is to allocate N*2 elements (usually).
So if you have 512 bytes reserved for use by the container and it needs more memory, one would allocate 1024 bytes, and the next realloc would be 2048.
My question is, why are you allocating a new array and copying every time you add a new element? Wouldn't it be better to allocate a chunk of memory, and allow access to it as needed?
The problem is here:
class PaintHandler{
};
That shouldn't be in your source file.
It's not managed C++ anymore, it's C++/CLI--there is a difference.
managed C++ had the horrible syntax, C++/CLI is actually kind of decent.
Oh yeah, I had a problem with that when I was first learning C++ too.
= assigns to whatever is on the left side of it.
Truthfully, my best advice to you is not make performance your #1 priority for every application.
Professional programmers, (and sometimes software engineers) will use the right tool for the job. That's what programming languages are, tools with which to solve problems. Native C++ (the ISO kind, not C++/CLI) is often used in performance-critical applications, and also is somewhat vendor independent. Many computer games are written with C++. Now having said that, it does have it's limitations, such as not having a standard way to create a Graphical User Interface. To create a GUI with native C++ will be different for Windows and for Linux. If you want to know more about C++ you can read the FAQ on Bjarne Stroustrup's (the creator of C++) website.
While C++ can be used for just about everything under the sun, and I do recommend you learn it if you intend to be a software engineer/developer/programmer--I myself would opt to use the RIGHT tool for the job.
Automating HTTP stuff is a real pain with C++, but the beauty of the beast is that there are so many libraries to assist with doing things like that (it's quite commonly used) it's almost a guarantee to find a library for doing many things from a quick google search.
Now I'm done ranting, but I want to say it is of utmost performance that you learn more than one programming language. .NET languages (like C# and C++/CLI) are Microsoft dependent, but allow …
The most likely reason they don't like .NET is because of vendor dependence, namely .NET code only working on Windows.
The majority of the world uses Windows, and if you fear that may change, or you like Linux oh so much then you should steer clear of it.
You will have to maintain an array of account numbers. In certain circumstances a programming teacher may accept a statically allocated array--when learning the basics of programming.
If you don't know how to use arrays, I suggest you learn how. Truthfully there are (probably) infinite ways to accomplish the task, however.
The code for templates must be only in a header file.
Which isn't exactly true, the other way is to include the .cpp file at the bottom of the .h
when doing that with visual studio you must also exclude the .cpp file from the solution explorer. Easiest/best to just include the code in the header file.
If you do that wrong you get link errors.
you can also use toupper() or tolower() in <cctype>
or use the falling-through behavior of the switch for 'Y' and 'y' etc.
Though, personally I would make a small function to test if it was valid.
May have to dynamically allocate the memory using "new" and "delete".
Also, in this line:
int fileTempSize = data.size();
int doesn't have the range of the unsigned int, make sure your fileTempSize is the same type as the type returned by data.size()
this can cause problems if the size is greater than std::numeric_limits<int>::max(), it will overflow the variable.
std::vector<char>::size_type fileTempSize = data.size();
Or if you have a compiler that supports the new "auto" keyword..
auto fileTempSize = data.size();