agentleFS
ROCmROCm/rocprofiler-systems-skills/skills/programming-cpp-stl-algorithms/SKILL.md

programming-cpp-stl-algorithms

Skill5 starsChanged 16 days agoMITView on GitHub

C++ STL algorithms and data structures - suggests appropriate containers and algorithms when they solve the problem at hand

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 4 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈4

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this skill when implementing C++ code that could benefit from STL containers or algorithms.

    line 8
  2. Distinctive:

    When you recognize a problem that an STL algorithm or container solves, suggest it to the user instead of writing manual loops or custom …

    line 11
  3. Distinctive:

    When you detect an algorithm opportunity, present it like this:

    line 257
  4. Distinctive:

    Use traditional STL algorithms with iterators instead.

    line 315

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

C++ STL Algorithms & Data Structures Skill

Use this skill when implementing C++ code that could benefit from STL containers or algorithms.

<IMPORTANT> When you recognize a problem that an STL algorithm or container solves, **suggest it to the user** instead of writing manual loops or custom implementations.

Prefer STL algorithms over raw loops - they are:

  • More readable (express intent)
  • Less error-prone
  • Often more efficient
  • Easier to parallelize</IMPORTANT>

Reference: CppReference - Algorithms

When to Suggest

Instead of Manual Loops

When you see...Suggest...
Loop to find an elementstd::find, std::find_if
Loop to check if any/all elements matchstd::any_of, std::all_of, std::none_of
Loop to count elementsstd::count, std::count_if
Loop to copy elementsstd::copy, std::copy_if
Loop to transform elementsstd::transform
Loop to accumulate/sum valuesstd::accumulate, std::reduce
Loop to remove elementsstd::remove, std::remove_if + erase
Loop to sortstd::sort, std::stable_sort, std::partial_sort
Loop to find min/maxstd::min_element, std::max_element, std::minmax_element
Loop to reversestd::reverse
Loop to fill with valuesstd::fill, std::generate
Loop to check if sortedstd::is_sorted
Nested loops for set operationsstd::set_union, std::set_intersection, std::set_difference

Data Structures - When to Use What

Sequence Containers

ContainerUse When...Complexity
std::vectorDefault choice, random access, add at endO(1) access, O(1) amortized push_back
std::arrayFixed size known at compile timeO(1) access, no heap allocation
std::dequeNeed to add/remove at both endsO(1) access, O(1) push_front/back
std::listFrequent insert/remove in middle, no random access neededO(1) insert/remove, O(n) access
std::forward_listSingly-linked list, minimal memoryO(1) insert after, forward iteration only

Associative Containers

ContainerUse When...Complexity
std::setUnique sorted elements, fast lookupO(log n) insert/find
std::mapKey-value pairs, sorted by keyO(log n) insert/find
std::multisetSorted elements with duplicatesO(log n) insert/find
std::multimapKey-value with duplicate keysO(log n) insert/find

Unordered Containers (Hash-based)

ContainerUse When...Complexity
std::unordered_setUnique elements, fastest lookup, order doesn't matterO(1) avg insert/find
std::unordered_mapKey-value, fastest lookup, order doesn't matterO(1) avg insert/find

Container Adaptors

ContainerUse When...
std::stackLIFO (Last In First Out)
std::queueFIFO (First In First Out)
std::priority_queueAlways access largest/smallest element

Algorithm Categories

Non-modifying Sequence Operations

// Finding elements
auto it = std::find(v.begin(), v.end(), value);
auto it = std::find_if(v.begin(), v.end(), [](int x) { return x > 5; });

// Checking conditions
bool any = std::any_of(v.begin(), v.end(), pred);
bool all = std::all_of(v.begin(), v.end(), pred);
bool none = std::none_of(v.begin(), v.end(), pred);

// Counting
int count = std::count(v.begin(), v.end(), value);
int count = std::count_if(v.begin(), v.end(), pred);

// Searching
auto it = std::search(v.begin(), v.end(), sub.begin(), sub.end());
auto it = std::adjacent_find(v.begin(), v.end());  // Find consecutive duplicates

Modifying Sequence Operations

// Copying
std::copy(src.begin(), src.end(), dest.begin());
std::copy_if(src.begin(), src.end(), std::back_inserter(dest), pred);

// Transforming
std::transform(v.begin(), v.end(), v.begin(), [](int x) { return x * 2; });
std::transform(a.begin(), a.end(), b.begin(), result.begin(), std::plus<>{});

// Filling
std::fill(v.begin(), v.end(), value);
std::generate(v.begin(), v.end(), []() { return rand(); });
std::iota(v.begin(), v.end(), 0);  // Fill with 0, 1, 2, 3...

// Removing (erase-remove idiom)
v.erase(std::remove(v.begin(), v.end(), value), v.end());
v.erase(std::remove_if(v.begin(), v.end(), pred), v.end());

// Replacing
std::replace(v.begin(), v.end(), old_val, new_val);
std::replace_if(v.begin(), v.end(), pred, new_val);

// Reversing
std::reverse(v.begin(), v.end());

// Rotating
std::rotate(v.begin(), v.begin() + n, v.end());  // Move first n elements to end

// Unique (remove consecutive duplicates)
v.erase(std::unique(v.begin(), v.end()), v.end());

Sorting and Related

// Sorting
std::sort(v.begin(), v.end());
std::sort(v.begin(), v.end(), std::greater<>{});  // Descending
std::sort(v.begin(), v.end(), [](auto& a, auto& b) { return a.name < b.name; });

std::stable_sort(v.begin(), v.end());  // Preserves relative order of equal elements
std::partial_sort(v.begin(), v.begin() + n, v.end());  // Only sort first n

// Nth element (partition around nth)
std::nth_element(v.begin(), v.begin() + n, v.end());  // nth element in sorted position

// Checking sorted
bool sorted = std::is_sorted(v.begin(), v.end());

// Binary search (requires sorted range)
bool found = std::binary_search(v.begin(), v.end(), value);
auto it = std::lower_bound(v.begin(), v.end(), value);  // First >= value
auto it = std::upper_bound(v.begin(), v.end(), value);  // First > value
auto [lo, hi] = std::equal_range(v.begin(), v.end(), value);  // Range of equal elements

Min/Max Operations

// Single values
int m = std::min(a, b);
int m = std::max({a, b, c, d});  // Initializer list
auto [lo, hi] = std::minmax(a, b);

// In containers
auto it = std::min_element(v.begin(), v.end());
auto it = std::max_element(v.begin(), v.end());
auto [min_it, max_it] = std::minmax_element(v.begin(), v.end());

// Clamping
int clamped = std::clamp(value, low, high);

Numeric Operations (<numeric>)

// Sum/accumulate
int sum = std::accumulate(v.begin(), v.end(), 0);
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<>{});

// Reduce (parallelizable version of accumulate, C++17)
int sum = std::reduce(v.begin(), v.end());

// Inner product (dot product)
int dot = std::inner_product(a.begin(), a.end(), b.begin(), 0);

// Partial sums (prefix sum)
std::partial_sum(v.begin(), v.end(), result.begin());

// Adjacent difference
std::adjacent_difference(v.begin(), v.end(), result.begin());

// Transform reduce (C++17)
auto result = std::transform_reduce(v.begin(), v.end(), init, binary_op, unary_op);

Set Operations (on sorted ranges)

std::vector<int> result;

// Union
std::set_union(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(result));

// Intersection
std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(result));

// Difference (in a but not in b)
std::set_difference(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(result));

// Symmetric difference (in a or b but not both)
std::set_symmetric_difference(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(result));

// Check if includes
bool includes = std::includes(a.begin(), a.end(), b.begin(), b.end());

// Merge sorted ranges
std::merge(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(result));

Heap Operations

// Create heap (max-heap by default)
std::make_heap(v.begin(), v.end());

// Add element to heap
v.push_back(value);
std::push_heap(v.begin(), v.end());

// Remove largest from heap
std::pop_heap(v.begin(), v.end());
v.pop_back();

// Sort heap
std::sort_heap(v.begin(), v.end());

// Check if heap
bool is_heap = std::is_heap(v.begin(), v.end());

Permutations

// Next/previous permutation
std::next_permutation(v.begin(), v.end());
std::prev_permutation(v.begin(), v.end());

// Shuffle
std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});

How to Suggest

When you detect an algorithm opportunity, present it like this:

STL Algorithm Suggestion

Instead of this manual loop:

for (auto it = v.begin(); it != v.end(); ++it) {
    if (*it == target) return it;
}

Consider using std::find:

auto it = std::find(v.begin(), v.end(), target);

Benefits: More readable, expresses intent, potentially optimized.

Common Patterns

Erase-Remove Idiom

// Remove all even numbers
v.erase(std::remove_if(v.begin(), v.end(), 
    [](int x) { return x % 2 == 0; }), v.end());

// C++20 only (NOT available): std::erase_if(v, pred);

Transform + Back Inserter

std::vector<std::string> names;
std::transform(people.begin(), people.end(), 
    std::back_inserter(names),
    [](const Person& p) { return p.name; });

Copy If + Back Inserter

std::vector<int> evens;
std::copy_if(v.begin(), v.end(), 
    std::back_inserter(evens),
    [](int x) { return x % 2 == 0; });

Accumulate with Custom Operation

// Concatenate strings
std::string result = std::accumulate(strings.begin(), strings.end(), 
    std::string{}, [](const std::string& a, const std::string& b) {
        return a.empty() ? b : a + ", " + b;
    });

C++20 Ranges (NOT AVAILABLE)

<IMPORTANT> This project uses **C++17**. C++20 Ranges are NOT available. Use traditional STL algorithms with iterators instead. </IMPORTANT>
// Use this (C++17) - traditional iterators
std::vector<int> evens;
std::copy_if(v.begin(), v.end(), std::back_inserter(evens),
    [](int x) { return x % 2 == 0; });
std::sort(evens.begin(), evens.end());

// NOT available (C++20 ranges) - DO NOT USE
// auto evens = v | std::views::filter(...) | std::ranges::to<...>();

References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.