In C++ STL, vector class is a template class of sequence containers that arrange elements of a given type in a linear arrangement and allow fast random access to any element. We speak of a vector as a container because it contains other objects. All of the objects in a container must have the same type. They should be the preferred container for a sequence when random-access performance is at a premium. Vector must has single type and each of which has an associated integer index. As in string, STL takes care of memory usage associated with storing all the elements. When we use CL.EXE with /MT, /MTd, /MD, or /MDd, we can also gain advantages from thread-safety effect point of view. The way to initialize a vector in C++ is easy as following:
vector<T> v1; // vector that holds objects of type T; Default constructor v1 is empty
vector<T> v2(v1); // v2 is a copy of v1
vector<T> v3(n, i); // v3 has n elements with value i
vector<T> v4(n); // v4 has n copies of a value-initialized object
Note: put #include<vector> and using std::vector before.
Vector operation in native STL is:
v.empty(); // Returns true if v is empty; otherwise returns false
v.size(); // Returns number of elements in v
v.push_back(t); // Adds element with value t to end of v
v
; // Returns element at position n in v
v1 = v2; // Replaces elements in v1 by a copy of elements in v2
v1 == v2; // Returns TRue if v1 and v2 are equal
!=, <, <=,
>, and >= // Have their normal meanings
With Visual C++ 2005, the Standard Template Library (STL) has been re-engineered to work under the .NET Framework. Stanley Lippman article was the first in a series of articles on STL.NET, a re-engineering of the STL implemented using both the CLI generic and the C++ template mechanisms. STL.NET is new to Visual C++ and only shipped in Visual Studio 2005. I agreed with him that STL.NET is a very exciting library addition, and I hope you will agree too. Let see MSDN example of STL.NET:
#include <cli/vector>
#include <algorithm>
void stlCollection()
{
vector<String^> ^svec = gcnew vector<String^>;
svec->push_back("Pooh");
svec->push_back("Piglet");
svec->push_back("Eeyore");
svec->push_back("Rabbit");
// generic algorithm: sort
sort( svec->begin(), svec->end() );
Console::WriteLine( "Collection holds {0} elements: ", svec->size() );
for ( int i = 0; i < svec->size(); i++ ) Console::WriteLine( svec[ i ] );
// generic algorithm: find
vector<String^>::iterator iter = find( svec->begin(), svec->end(), "Pooh" );
if ( iter != svec->end() )
{
// no downcast required ...
String ^item = *iter;
svec->erase( iter );
}
// generic algorithm: remove ...
remove( svec->begin(), svec->end(), "Rabbit" );
Console::WriteLine( "\nCollection holds {0} elements:", svec->size() );
IEnumerator<String^> ^is = svec->GetEnumerator();
while ( is->MoveNext()) Console::WriteLine( is->Current );
}
Ok. Before you finish read this posting, be carefull with vector structure in .NET System.Windows namespace (WindowsBase.dll). It is a different vector animal. It is used by WPF to represents a displacement in 2-D space. Any public static members of vector type are thread safe but any instance members are not guaranteed to be thread safe.
I hope you enjoy STL as I do.
Thx - R.A.M