PrevUpHomeNext

c++ STL


Install STL

c++ STL: standard Template Library is provided by the c++ compiler. After installing a c++ compiler, stl is already ready.

Use STL

Some examples (gcc for example)

std::vector

#include <vector>

int main()
{
	std::vector<float> vector{2.3f, 3.4f, 1.2f};
}

Compile it:

g++ prog.cpp -std=c++26 -o prog

std::copy

Copy memory with std::copy

#include <algorithm>
#include <iostream>

int main()
{
	char str1[] = "Hello, c++!";
	unsigned int size = sizeof str1;
	std::string str2;
	str2.resize(size);
	std::copy(str1, str1+size, str2.begin());
	std::cout << "str2 is " << str2 << std::endl;
	// str2 is Hello, c++!
}

Compile it:

alias gpp="g++ -std=c++26"
gpp prog.cpp -o prog
./prog

std::fill

Fill memory with std::fill

#include <algorithm>
#include <iostream>

int main()
{
	char * str = new char[11];
	std::fill(str, str+10, 'R');
	str[10] = '\0';
	std::cout << str << std::endl;
	// RRRRRRRRRR
}

Compile it:

alias gpp="g++ -std=c++26"
gpp prog.cpp -o prog
./prog

Date

Apr 12, 2025

Back

Up

Deck

c++ Toolchain

@cppfx.xyz


PrevUpHomeNext