You don't actually have an array. I'm not sure what you think char dirlist[] = {}; does.
You need to replace it with a container that can grow. There are many, your question suggests a vector, an array that can grow. You're storing strings in the container.
You need to include vector and string as:
1 2
#include <vector>
#include <string>
You need to replace the old iostream.h with the new iostream as:
1 2
#include <iostream>
usingnamespace std;
You need to replace char dirlist[] = {}; with vector<string> dirlist;
Finally, you need to put elements into the container. Replace: dirlist[i] = *dir->d_name;
with: dirlist.push_back(dir->d_name);
And how to show the output? If I use the cout I get this error: C:\cpp_proj\temptest\main.cpp|24|error: no match for'operator<<' in 'std::cout << dirlist'
And if I use the printf(); a runtime error occurred.
To display an element from vector, use [] operator: cout <<dirlist[0]; or cout <<dirlist.at(0); // display the first element in vector
Use for loop to display contents of dirslist:
Seriously I wonder if the STL implementers can do a "faster" job than our for-loop but Scott Meyers in Effective C++ says it is always good to turn to STL algorithm as it has a "chance" to do a better job than our for-loop as they know the internal implementation of the vector class.
I don't know if I should agree with this recommendation. Anyone dis-agree ? Can a copy algorithm beat a simple for-loop in our case ? Hmmm....