Anonymous user
Array Initialization: Difference between revisions
undo edit 22834
(undo edit 22834) |
|||
Line 67:
for(int x = 0; x < 10; ++x)
myArray[x][y] = (x+1) * (y+1);
</lang>▼
===STL===▼
{{libheader|STL}}STL provides '''std::vector''', which behaves as a dynamically-resizable array. When an element is added, its value must be set immediately.▼
<lang cpp>▼
myVector.push_back(value);▼
</lang>▼
Like simple arrays, '''std::vector''' allows the use of the [] operator, and once an element has been added, it can be changed the same way a simple array can.▼
<lang cpp>▼
myVector[0] = value;▼
</lang>▼
Unlike simple arrays, '''std::vector''' allows you to determing the size of the array. You can use this set all of the values in the array:▼
<lang cpp>▼
// Create a list of numbers from 1 to the size of the vector.▼
size_t size = myVector.size();▼
for(int i = 0; i < size; ++i)▼
myVector[i] = i + 1;▼
</lang>▼
'''std::vector''' also provides iterators, allowing you to iterate through a vector's elements the same way you might any other STL container class.▼
// Create a list of numbers from 1 to the size of the vector.▼
<lang cpp>▼
std::vector<int> myVector;▼
int val = 0;▼
for(std::vector<int>::iterator it = myVector.begin();▼
it != myVector.end();▼
++it)▼
*it = ++val;▼
</lang>▼
A vector can also explicitly be resized:▼
<lang cpp>▼
std::vector<int> myVector;▼
myVector.resize(10); // now the vector contains 10 elements, all of which are 0▼
myVector.resize(15, 3); // now the vector contains 15 elements, the 5 new got the value 3▼
myVector.resize(12); // the last three elements got removed▼
</lang>▼
Also note that a vector can already be filled at construction time:▼
<lang cpp>▼
std::vector v1(10); // a vector of 10 ints, all initialized with 0▼
std::vector v2(5, 7); // a vector containing 5 ints, all inizialized with 7▼
int a[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };▼
std::vector v3(a, a+10); // a vector containing 10 ints, initialized with the elements of a (i.e. v3[0]==a[0] etc.)▼
std::vector v4 = v3; // v4 is a copy of v3▼
</lang>
Line 252 ⟶ 294:
for x in range(height):
myArray.append([0] * width)
▲</lang>
▲===STL===
▲{{libheader|STL}}STL provides '''std::vector''', which behaves as a dynamically-resizable array. When an element is added, its value must be set immediately.
▲<lang cpp>
▲myVector.push_back(value);
▲</lang>
▲Like simple arrays, '''std::vector''' allows the use of the [] operator, and once an element has been added, it can be changed the same way a simple array can.
▲<lang cpp>
▲myVector[0] = value;
▲</lang>
▲Unlike simple arrays, '''std::vector''' allows you to determing the size of the array. You can use this set all of the values in the array:
▲<lang cpp>
▲// Create a list of numbers from 1 to the size of the vector.
▲size_t size = myVector.size();
▲for(int i = 0; i < size; ++i)
▲ myVector[i] = i + 1;
▲</lang>
▲'''std::vector''' also provides iterators, allowing you to iterate through a vector's elements the same way you might any other STL container class.
▲ // Create a list of numbers from 1 to the size of the vector.
▲<lang cpp>
▲std::vector<int> myVector;
▲int val = 0;
▲for(std::vector<int>::iterator it = myVector.begin();
▲ it != myVector.end();
▲ ++it)
▲ *it = ++val;
▲</lang>
▲A vector can also explicitly be resized:
▲<lang cpp>
▲std::vector<int> myVector;
▲myVector.resize(10); // now the vector contains 10 elements, all of which are 0
▲myVector.resize(15, 3); // now the vector contains 15 elements, the 5 new got the value 3
▲myVector.resize(12); // the last three elements got removed
▲</lang>
▲Also note that a vector can already be filled at construction time:
▲<lang cpp>
▲std::vector v1(10); // a vector of 10 ints, all initialized with 0
▲std::vector v2(5, 7); // a vector containing 5 ints, all inizialized with 7
▲int a[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
▲std::vector v3(a, a+10); // a vector containing 10 ints, initialized with the elements of a (i.e. v3[0]==a[0] etc.)
▲std::vector v4 = v3; // v4 is a copy of v3
</lang>
|