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 << ' '; }
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 6.67
Transform and Generate Algorithms
Svep för att visa menyn
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 << ' '; }
Tack för dina kommentarer!