Yes, basically std::fill_n is like a for loop. It can be used with any collection that is iterable, which if it's a C++ collection it is. The definition requires something that falls into the type "OutputIterator".
OutputIterator is more specialized than just an array or collection, like vector<&T>. Collections usually have iterators or can be wrapped in an iterator in order to iterate over the collection.
On that page I linked you to for the definition of std::fill_n it states that the following is the defined behavior for the function:
- Code: Select all
template < class OutputIterator, class Size, class T >
void fill_n ( OutputIterator first, Size n, const T& value )
{
for (; n>0; --n) *first++ = value;
}
Anyway that's probably why it requires the number of items to fill.
I was working on a more complete version of the text-mode TicTacToe based on your idea. You mentioned that I had just used one file, which does simplify the program significantly. Once you start using header files then the whole solution becomes more complex, but it is also easier to maintain. Here is a link to my version of TicTacToe so far, which is closer to what you're looking for. The only thing missing right now is the function that determines a winner:
http://dl.dropbox.com/u/10646479/TicTacToe.zipI'm still looking at your code, because everything I've read says it should work. I haven't tried to run it yet though.
-- Wed Jan 05, 2011 10:15 am --
Alright I just wrote a simple program to test passing an array of ints around and manipulating it.
- Code: Select all
#include <iostream>
int intArray[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
void printArray(int * array, int size)
{
for (int arrayIndex = 0; arrayIndex < size; arrayIndex++)
{
if (arrayIndex > 0)
{
std::cout << ", ";
}
std::cout << array[arrayIndex];
}
std::cout << std::endl;
}
void manipulateArray(int * intArray)
{
intArray[1] = 1;
intArray[3] = 1;
intArray[5] = 1;
intArray[7] = 1;
}
int main(int argc, char * argv[])
{
printArray(intArray, 9);
intArray[0] = 24;
printArray(intArray, 9);
manipulateArray(intArray);
printArray(intArray, 9);
}
This program's output looks like:
- Code: Select all
0, 0, 0, 0, 0, 0, 0, 0, 0
24, 0, 0, 0, 0, 0, 0, 0, 0
24, 1, 0, 1, 0, 1, 0, 1, 0
Which is what I would expect. And I thought that was basically what you are doing. So I need to look at your code some more.
-- Wed Jan 05, 2011 2:24 pm --
BTW I finished up the text mode Tic Tac Toe game (Same Dropbox URL as before):
http://dl.dropbox.com/u/10646479/TicTacToe.zip