STL Container
vector, map, set, stack, queue, list, deque ...
STL container is a standard interface for all template-based containers. It allows for automatic memory management.
Creating Vectors
Vector is a dynamic array that can hold any data type. All elements in a vector are located in contiguous memory. Memory is automatically managed as the number of elements increase. You can randomly access to any of elements (random access).
#include <iostream>
#include <vector>
int main()
{
std::vector<int> scores;
scores.reserve(2);
scores.push_back(30);
scores.push_back(50);
scores.pop_back();
std::cout << "Current capacity : " << scores.capacity() << std::endl;
std::cout << "Current size: " << scores.size() << std::endl;
}
#include <iostream>
#include <vector>
int main()
{
std::vector<int> scores;
scores.reserve(2);
scores.push_back(1);
scores.push_back(2);
std::vector<int> scores1(scores); // copy of scores
}
// not good
#include <iostream>
#include <vector>
int main()
{
std::vector<int> scores(10);
std::vector<Cat> myCats(4);
// ...
}
If you set the size of a vector, all elements are set as 0.
Push and Pop
// push_back(data);
scores.push_back(30);
names.push_back("Coco");
myCats.push_back(myNewCat);
// pop_back();
scores.pop_back();
Capacity and Size
Capacity is the number of usable space for elements. Size is the number of e lements that are actually present.
scores.capacity();
scores.size();
reserve(size); increases the capacity of the vector. When it does, it allocates new memory and copies original elements to new memory. So, call the reserve function immediately after creating the vector to avoid unnecessary reallocation.
scores.reserve(10);
Access to an Element
scores[i] = 3;
std::cout << names[i] << " ";
std::cout << myCats[i].GetScore() << " ";
It returns the element at the specified position by reference.
Iterator
#include <iostream>
#include <vector>
int main()
{
std::vector<int> scores;
scores.reserve(2);
scores.push_back(30);
scores.push_back(50);
for (std::vector<int>::iterator iter = scores.begin(); iter != scores.end(); ++iter)
{
std::cout << *iter << " ";
}
// ...
}
vector<int>::iterator bIter = scores.begin();
vector<int>::iterator eIter = scores.end();
vector<int>::reverse_iterator reversedBeginIt = scores.rbegin();
vector<int>::reverse_iterator
Inserting and Erasing
std::vector<int> scores;
scores.reserve(4);
scores.push_back(10);
scores.push_back(50);
scores.push_back(38);
scores.push_back(100); // 10, 50, 38, 100
std::vector<int>::iterator it = scores.begin();
it = scores.insert(it, 80); // 80, 10, 50, 38, 100
std::vector<int> scores;
scores.reserve(4);
scores.push_back(10);
scores.push_back(50);
scores.push_back(38);
scores.push_back(100); // 10, 50, 38, 100
std::vector<int>::iterator it = scores.begin();
it = scores.erase(it); // 50, 38, 100
It is slow because copying and sometimes reallocation takes place.
Swaping
// Main.cpp
vector<int> scores;
scores.reserve(2);
scores.push_back(85); // 85
scores.push_back(73); // 85, 73
vector<int> anotherScores;
anotherScores.assign(7,100); // 100, 100, 100, 100, 100, 100, 100
scores.swap(anotherScores); // scores: 100, 100, 100, 100, 100, 100, 100
// anotherScores: 85, 73
Resizing
std::vector<int> scores;
scores.reserve(3);
scores.push_back(30);
scores.push_back(100);
scores.push_back(70); // 30, 100, 70
scores.resize(2); // 30, 100
for (int i=0; i < scores.size(); ++i)
{
std::cout << scores[i] << " "; // "30 100"
}
The function resize change the size of a vector. If new size is smaller than the original size, it removes excess. If new size is bigger than the original size, it reassigns memory.
Clear( )
scores.clear(); // size: 0 capacity: no change
Object Vector
// Score.h
class Score
{
public:
// ...
private:
int mScore;
string mClassName;
};
// Main.cpp
int main()
{
vector<Score> scores;
scores.reserve(4);
scores.push_back(Score(30, "C++"));
scores.push_back(Score(59, "Algorithm"));
scores.push_back(Score(87, "Java"));
scores.push_back(Score(41, "Android"));
// ...
}
Pointer Vector
vector<Score*> scores;
scores.reserve(1);
scores.push_back(new Score(10, "C++")); // score: 1 1 address(points the object)
scores.push_back(new Score(41, "Android")); // score: 2 2 address(points pointers)
for (vector<Score*>::iterator iter = scores.begin(); iter != scores.end(); ++iter)
{
delete *iter; // Don't forget!
}
scores.clear();
Pros and Cons of Vector
Pros
- Elements can be accessed randomly in any order. (If you know the index.)
- You can quickly push and pop elements in the last position.
Cons
- Inserting or erasing elements in the middle of a vector takes a long time.
- There is a cost to reassign and copy elements.
'College Study > C++ (ENG)' 카테고리의 다른 글
[C++] Standard Template Library Container: Others (ENG) (0) | 2022.01.02 |
---|---|
[C++] Standard Template Library Container: Map (ENG) (0) | 2022.01.02 |
[C++] Exception (ENG) (0) | 2022.01.02 |
[C++] Static Keyword (ENG) (0) | 2022.01.02 |
[C++] Inline Functions (ENG) (0) | 2022.01.02 |
댓글