Transform and Generate Algorithms
Transform and generate algorithms in C++ enable functional-style operations that modify or create data directly within containers.
The STL provides two key functions for this purpose — std::transform and std::generate. std::transform applies a function to each element in a range to produce modified results, while std::generate fills a range using a function that supplies new values. Together, they make code more concise, expressive, and easier to maintain.
These algorithms help you write clear, maintainable code that leverages the full power of the STL.
main.cpp
1234567891011121314151617#include <iostream> #include <vector> #include <algorithm> #include <ranges> int square(int x) { return x * x; } int main() { std::vector<int> data{1, 2, 3, 4, 5}; std::vector<int> result(data.size()); std::transform(data.begin(), data.end(), result.begin(), square); for (int n : result) std::cout << n << " "; }
When you need to fill a container with values computed dynamically, use std::generate. It applies a generator function to fill each element in a range — perfect for creating random numbers, sequences, or computed values without manual loops. Together with std::transform, it helps write cleaner, more expressive code.
main.cpp
12345678910111213#include <iostream> #include <vector> #include <algorithm> #include <cstdlib> int main() { std::vector<int> numbers(5); std::generate(numbers.begin(), numbers.end(), rand); // Fill with random values for (int n : numbers) std::cout << n << ' '; }
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår
Awesome!
Completion rate improved to 6.67
Transform and Generate Algorithms
Sveip for å vise menyen
Transform and generate algorithms in C++ enable functional-style operations that modify or create data directly within containers.
The STL provides two key functions for this purpose — std::transform and std::generate. std::transform applies a function to each element in a range to produce modified results, while std::generate fills a range using a function that supplies new values. Together, they make code more concise, expressive, and easier to maintain.
These algorithms help you write clear, maintainable code that leverages the full power of the STL.
main.cpp
1234567891011121314151617#include <iostream> #include <vector> #include <algorithm> #include <ranges> int square(int x) { return x * x; } int main() { std::vector<int> data{1, 2, 3, 4, 5}; std::vector<int> result(data.size()); std::transform(data.begin(), data.end(), result.begin(), square); for (int n : result) std::cout << n << " "; }
When you need to fill a container with values computed dynamically, use std::generate. It applies a generator function to fill each element in a range — perfect for creating random numbers, sequences, or computed values without manual loops. Together with std::transform, it helps write cleaner, more expressive code.
main.cpp
12345678910111213#include <iostream> #include <vector> #include <algorithm> #include <cstdlib> int main() { std::vector<int> numbers(5); std::generate(numbers.begin(), numbers.end(), rand); // Fill with random values for (int n : numbers) std::cout << n << ' '; }
Takk for tilbakemeldingene dine!