A little tutorial on how to use vectors.
Declaration:
Code:
vector<elemType> vecName; -- Creates the empty vector object vecName without any elements
vector<elemType> vecName(otherVecName); -- Creates the vector object vecName and initializes vecName to the elements of the vector otherVecName. vecName and othervecName are of the same type.
vector<elemType> vecName(size); -- Creates the vector object vecName of size size. vecName is initialized using the defualt values.
vector<elemType> vecName(n, elem); -- Creates the vecotr object vecName of size n. vecName is initialized using n copies of the element elem.
There are only certain functions you can use on vector containers. Which are:
Code:
vecName.at(index) -- Returns the element at the position specified by index.
vecName.[index] -- Returns the element at the position specified by index.
vecName.front() -- Returns the first element. (Does not check whether the object is empty.)
vecName.back() -- Returns the last element. (Does not check whether the object is empty.)
vecName.clear() -- Deletes all elements from the object.
vecName.push_back(elem) -- A copy of elem is inserted into vecName at the end.
vecName.pop_back() -- Delete the last element of vecName.
vecName.empty() -- Returns true if the object vecName is empty and false otherwise.
vecName.size() -- Returns the number of elements currently in
vecName.maxsize() -- Returns the maximum number of elements that can be inserted into the object vecNames.
This teaches you how to list out the elements of the vector container.
Code:
#undef UNICODE
#include <vector>
#include <string>
#include <windows.h>
#include <Tlhelp32.h>
#include <iostream>
using namespace std;
int main(void)
{
vector<string>processNames;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hTool32 = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
BOOL bProcess = Process32First(hTool32, &pe32);
if(bProcess == TRUE)
{
while((Process32Next(hTool32, &pe32)) == TRUE)
processNames.push_back(pe32.szExeFile);
}
CloseHandle(hTool32);
cout << "Number of Processes: " << processNames.size() << endl;
cin.ignore(1);
for(int j = 0; j < processNames.size(); j++)
cout << processNames[j] << " " << endl;
cout << endl;
cout << "Press Enter To Exit: " << endl;
cin.ignore(1);
return 0;
}
The #undef UNICODE disables unicode within the vector container. Otherwise you will get compile errors on visual studio 2010.
-Bit_Hacker