0% found this document useful (0 votes)
14 views10 pages

CPP Header Vector STL Guide

This document serves as a guide to various C++ header files and the Vector Standard Template Library (STL). It provides examples and explanations for header files such as <iostream>, <cmath>, <vector>, and others, detailing their functionalities and common operations. Additionally, it covers vector operations and traversal methods in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

CPP Header Vector STL Guide

This document serves as a guide to various C++ header files and the Vector Standard Template Library (STL). It provides examples and explanations for header files such as <iostream>, <cmath>, <vector>, and others, detailing their functionalities and common operations. Additionally, it covers vector operations and traversal methods in C++.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C++ Header Files and Vector STL Guide

<iostream>

Input/output stream. Used for cin, cout.

Example:

#include <iostream>

using namespace std;

int main() {

cout << "Hello, world!" << endl;

return 0;

<cmath>

Mathematical functions like sqrt, pow.

Example:

#include <cmath>

#include <iostream>

using namespace std;

int main() {

cout << sqrt(25);

return 0;

<cstring>

C-style string functions.


C++ Header Files and Vector STL Guide

Example:

#include <cstring>

#include <iostream>

using namespace std;

int main() {

char str[20];

strcpy(str, "Hello");

cout << strlen(str);

return 0;

<cstdlib>

General utilities like rand, exit.

Example:

#include <cstdlib>

#include <iostream>

using namespace std;

int main() {

cout << rand();

return 0;

<string>
C++ Header Files and Vector STL Guide

String class support.

Example:

#include <string>

#include <iostream>

using namespace std;

int main() {

string s = "Test";

cout << s.length();

return 0;

<vector>

Dynamic array.

Example:

#include <vector>

#include <iostream>

using namespace std;

int main() {

vector<int> v = {1, 2};

v.push_back(3);

cout << v[2];

return 0;

}
C++ Header Files and Vector STL Guide

<algorithm>

Standard algorithms like sort, find.

Example:

#include <algorithm>

#include <vector>

#include <iostream>

using namespace std;

int main() {

vector<int> v = {3,1,2};

sort(v.begin(), v.end());

for(int x:v) cout << x << " ";

return 0;

<map>

Associative container for key-value pairs.

Example:

#include <map>

#include <iostream>

using namespace std;

int main() {

map<string, int> m;

m["a"] = 1;
C++ Header Files and Vector STL Guide

cout << m["a"];

return 0;

<set>

Stores unique sorted elements.

Example:

#include <set>

#include <iostream>

using namespace std;

int main() {

set<int> s = {2, 1, 1};

for(int x:s) cout << x << " ";

return 0;

<stack>

LIFO structure.

Example:

#include <stack>

#include <iostream>

using namespace std;

int main() {
C++ Header Files and Vector STL Guide

stack<int> s;

s.push(1);

cout << s.top();

return 0;

<queue>

FIFO structure.

Example:

#include <queue>

#include <iostream>

using namespace std;

int main() {

queue<int> q;

q.push(10);

cout << q.front();

return 0;

<deque>

Double-ended queue.

Example:

#include <deque>
C++ Header Files and Vector STL Guide

#include <iostream>

using namespace std;

int main() {

deque<int> d;

d.push_front(1);

cout << d.front();

return 0;

<bitset>

Bit operations.

Example:

#include <bitset>

#include <iostream>

using namespace std;

int main() {

bitset<4> b("1010");

cout << b;

return 0;

<ctime>

Date and time utilities.


C++ Header Files and Vector STL Guide

Example:

#include <ctime>

#include <iostream>

using namespace std;

int main() {

time_t now = time(0);

cout << ctime(&now);

return 0;

<iomanip>

Formatted output.

Example:

#include <iomanip>

#include <iostream>

using namespace std;

int main() {

double pi = 3.14159;

cout << fixed << setprecision(2) << pi;

return 0;

Vector STL in C++


C++ Header Files and Vector STL Guide

vector is a dynamic array in C++. It automatically resizes when needed.

Declaration:

vector<int> v;

vector<int> v(5, 10); // {10, 10, 10, 10, 10}

Common operations:

v.push_back(x) // Add at end

v.pop_back() // Remove last

v.size() // Number of elements

v.clear() // Remove all

v.empty() // Check if empty

v.front(), v.back() // First and last

Traversal:

for (int x : v) cout << x;

for (int i = 0; i < v.size(); i++) cout << v[i];

Full example:

#include <iostream>

#include <vector>

using namespace std;

int main() {

vector<int> v = {10, 20, 30};

v.push_back(40);
C++ Header Files and Vector STL Guide

for (int x : v) cout << x << " ";

return 0;

You might also like