mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 14:16:07 -06:00
New Core Tutorial
git-svn-id: svn://ultimatepp.org/upp/trunk@10538 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
parent
dd88feaf2e
commit
6c22e727de
37 changed files with 3125 additions and 0 deletions
58
tutorial/CoreTutorial/Vector.cpp
Normal file
58
tutorial/CoreTutorial/Vector.cpp
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#include "Tutorial.h"
|
||||
|
||||
void Vector1()
|
||||
{
|
||||
/// .`Vector` basics
|
||||
|
||||
/// `Vector` is the basic container of U++. It is the random access container similar to
|
||||
/// `std::vector` with one important performance related difference: There are rules for
|
||||
/// elements of `Vector` that allow its implementation to move elements in memory using
|
||||
/// plain `memcpy`/`memmove` ("Moveable" concept).
|
||||
|
||||
/// Anyway, for now let us start with simple `Vector` of `int`s:
|
||||
|
||||
Vector<int> v;
|
||||
|
||||
/// You can add elements to the Vector as parameters of the Add method
|
||||
|
||||
v.Add(1);
|
||||
v.Add(2);
|
||||
|
||||
DUMP(v);
|
||||
|
||||
/// Alternative and very important possibility for U++ containers is 'in-place creation'.
|
||||
/// In this case, parameter-less Add returns a reference to a new element in `Vector`:
|
||||
|
||||
v.Add() = 3;
|
||||
|
||||
DUMP(v);
|
||||
|
||||
/// You can also use `operator<<`
|
||||
|
||||
v << 4 << 5;
|
||||
|
||||
DUMP(v);
|
||||
|
||||
/// `Vector` also supports initializer lists:
|
||||
|
||||
v.Append({ 6, 7 });
|
||||
|
||||
DUMP(v);
|
||||
|
||||
/// To iterate `Vector` you can use indices:
|
||||
|
||||
for(int i = 0; i < v.GetCount(); i++)
|
||||
LOG(v[i]);
|
||||
|
||||
/// begin/end interface:
|
||||
|
||||
for(auto q = v.begin(), e = v.end(); q != e; q++)
|
||||
LOG(*q);
|
||||
|
||||
/// C++11 range-for syntax:
|
||||
|
||||
for(const auto& q : v)
|
||||
LOG(q);
|
||||
|
||||
///
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue