[ Web Proxy ]
URL:
Viewing: http://ru.cppreference.com/cpp/string/basic_string/resize_and_overwrite [Back]  [Original]

std::basic_string<CharT,Traits,Allocator>::resize_and_overwrite cppreference.com
cppreference.com

std::basic_string<CharT,Traits,Allocator>::resize_and_overwrite

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
std::basic_string
-
(C++17)
,
/
( C++20)( C++20)( C++20)( C++20)( C++20)(C++20)
(C++11)(C++11)(C++11)
(C++11)(C++11)
(C++11)(C++11)(C++11)
(C++11)
(C++11)
 
<tbody> </tbody>
template< class Operation > constexpr void resize_and_overwrite( size_type count, Operation op );
( C++23)

, count , op . std::string , , , C API.

:

  1. , count + 1 , k k *this, k count this->size() resize_and_overwrite. p .
    • this->compare(0, k, p, k) == 0.
    • [p + k, p + count] .
  2. std::move(op)(p, count). r std::move(op)(p, count).
  3. *this [p, p + r) ( *this r). [p, p + count].

, r . , std::move(op)(p, count) p count, r [0, count], [p, p + r) .

, , p , *this , *this, count this->capacity().

count
op ,

()

std::length_error, count > this->max_size(). , Allocator.

std::move(op)(p, count) , . , , .

resize_and_overwrite , *this, , . , resize_and_overwrite.

__cpp_lib_string_resize_and_overwrite 202110L (C++23) std::basic_string::resize_and_overwrite

: compiler explorer.

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <string_view>
static_assert(__cpp_lib_string_resize_and_overwrite);

constexpr std::string_view fruits[]{"apple", "banana", "coconut", "date", "elderberry"};

int main()
{
    //  :   fruits[0].    .
    std::string s{"Food: "};
    s.resize_and_overwrite(16, [sz = s.size()](char* buf, std::size_t buf_size)
    {
        const auto to_copy = std::min(buf_size - sz, fruits[0].size());
        std::memcpy(buf + sz, fruits[0].data(), to_copy);
        return sz + to_copy;
    });
    std::cout << "1. " << std::quoted(s) << '\n';

    //    .  ,  -
    //   .

    s.resize_and_overwrite(10, [](char* buf, int n)
    {
        return std::find(buf, buf + n, ':') - buf;
    });
    std::cout << "2. " << std::quoted(s) << '\n';

    std::cout << "3.     ,    . "
                 "   .\n";
    std::string food{"Food:"};
    const auto resize_to{27};
    std::cout << ", food.size: " << food.size()
              << ", food.capacity: " << food.capacity()
              << ", resize_to: " << resize_to
              << ", food: " << std::quoted(food) << '\n';

    food.resize_and_overwrite(
        resize_to,
        [food_size = food.size()](char* p, std::size_t n) noexcept -> std::size_t
        {
            // p[0]..p[n]  
            // p[0]..p[min(n, food_size) - 1]   
            // (    )

            //  :
            std::cout << " Operation(); n: " << n << '\n';

            //  fruits   p,   .
            char* first = p + food_size;

            for (char* const end = p + n; const std::string_view fruit : fruits)
            {
                char* last = first + fruit.size() + 1;
                if (last > end)
                    break;
                *first++ = ' ';
                std::ranges::copy(fruit, first);
                first = last;
            }

            const auto final_size{static_cast<std::size_t>(first - p)};

            //  :
            std::cout << " Operation(); final_size: " << final_size << '\n';

            assert(final_size <= n);
            return final_size; //      
                               // .      0..n.
        });

    std::cout << ", food.size: " << food.size()
              << ", food.capacity: " << food.capacity()
              << ", food: " << std::quoted(food) << '\n';
}

:

1. "Food: apple"
2. "Food"
3.     ,    .    .
, food.size: 5, food.capacity: 15, resize_to: 27, food: "Food:"
 Operation(); n: 27
 Operation(); final_size: 26
, food.size: 26, food.capacity: 30, food: "Food: apple banana coconut"


(public -) []

Web Proxy Viewer  |  New URL  |  Original Page