std::replace

std::replace

template <class ForwardIterator, class T>
  void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value);
 
 
  
Replace value in range
Assigns new_value to all the elements in the range [first,last) that compare equal to old_value. The function uses operator== to compare the individual elements to old_value. The behavior of this function template is equivalent to:
1
2
3
4
5
6
7
8
9
template <class ForwardIterator, class T>
  void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value)
{
  while (first!=last) {
    if (*first == old_value) *first=new_value;
    ++first;
  }
}


Parameters

first, last
Forward iterators to the initial and final positions in a sequence of elements that support being compared and assigned a value of type  T. The range used is  [first,last), which contains all the elements between  first and  last, including the element pointed by  first but not the element pointed by last.
old_value
Value to be replaced.
new_value
Replacement value.

Return value

none
#include <iostream>
#include <algorithm>
#include <vector>

int main() {

  int arr[] = { 11, 22, 30, 22, 55 };
  std::vector<int> v(arr, arr + 5);

  std::replace(v.begin(), v.end(), 22, 66);
  std::cout << "v: ";

  for (std::vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter)
    std::cout << " " << *iter;

  std::cout << "\n";
  return 0;

}


#include <iostream> #include <vector> #include <algorithm> #include <functional> // std::greater #include <string> #include <cctype> int main() { std::vector<int> vec = {5, 2, 8, 2, 9, 1, 5, 5}; std::vector<int> vec2(8); std::vector<std::string> words = {"apple", "Banana", "cherry"}; // 1. sort 排序 std::sort(vec.begin(), vec.end()); std::cout << "Sorted: "; for (int x : vec) std::cout << x << " "; std::cout << "\n"; // 2. reverse 反转 std::reverse(vec.begin(), vec.end()); std::cout << "Reversed: "; for (int x : vec) std::cout << x << " "; std::cout << "\n"; // 3. find 查找 auto it = std::find(vec.begin(), vec.end(), 8); if (it != vec.end()) { std::cout << "Found 8 at index: " << std::distance(vec.begin(), it) << "\n"; } // 4. count 统计 int cnt = std::count(vec.begin(), vec.end(), 5); std::cout << "Count of 5: " << cnt << "\n"; // 5. transform 转换(例如平方) std::transform(vec.begin(), vec.end(), vec2.begin(), [](int x) { return x * x; }); std::cout << "Transformed (squared): "; for (int x : vec2) std::cout << x << " "; std::cout << "\n"; // 6. replace 替换 std::replace(vec2.begin(), vec2.end(), 25, 100); // 把所有25换成100 std::cout << "After replace 25 -> 100: "; for (int x : vec2) std::cout << x << " "; std::cout << "\n"; // 7. unique 去除相邻重复(必须先排序) std::sort(vec.begin(), vec.end()); auto new_end = std::unique(vec.begin(), vec.end()); vec.erase(new_end, vec.end()); // 实际删除 std::cout << "After unique: "; for (int x : vec) std::cout << x << " "; std::cout << "\n"; // 8. min_element / max_element auto min_it = std::min_element(vec.begin(), vec.end()); auto max_it = std::max_element(vec.begin(), vec.end()); std::cout << "Min: " << *min_it << ", Max: " << *max_it << "\n"; // 9. binary_search (需要有序) bool found = std::binary_search(vec.begin(), vec.end(), 8); std::cout << "Binary search for 8: " << (found ? "found" : "not found") << "\n"; // 10. lower_bound / upper_bound auto lb = std::lower_bound(vec.begin(), vec.end(), 5); auto ub = std::upper_bound(vec.begin(), vec.end(), 5); std::cout << "Lower bound of 5 at index: " << std::distance(vec.begin(), lb) << "\n"; std::cout << "Upper bound of 5 at index: " << std::distance(vec.begin(), ub) << "\n"; // 11. next_permutation std::string s = "abc"; do { std::cout << s << " "; } while (std::next_permutation(s.begin(), s.end())); std::cout << "\n"; // 12. copy std::vector<int> copy_vec(vec.size()); std::copy(vec.begin(), vec.end(), copy_vec.begin()); std::cout << "Copy: "; for (int x : copy_vec) std::cout << x << " "; std::cout << "\n"; // 13. fill std::fill(vec2.begin(), vec2.end(), 0); std::cout << "After fill with 0: "; for (int x : vec2) std::cout << x << " "; std::cout << "\n"; // 14. equal 判断两个范围是否相同 bool same = std::equal(vec.begin(), vec.end(), copy_vec.begin()); std::cout << "vec and copy_vec are " << (same ? "equal" : "not equal") << "\n"; // 15. 自定义比较函数(降序排序) std::sort(copy_vec.begin(), copy_vec.end(), std::greater<int>()); std::cout << "Descending order: "; for (int x : copy_vec) std::cout << x << " "; std::cout << "\n"; // 16. find_if 使用谓词 auto even_it = std::find_if(copy_vec.begin(), copy_vec.end(), [](int x) { return x % 2 == 0; }); if (even_it != copy_vec.end()) { std::cout << "First even number: " << *even_it << "\n"; } // 17. count_if int even_count = std::count_if(copy_vec.begin(), copy_vec.end(), [](int x) { return x % 2 == 0; }); std::cout << "Number of even numbers: " << even_count << "\n"; return 0; } 代码中需要有简要注释函数功能
10-23
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值