>  기사  >  백엔드 개발  >  문자열 벡터를 내파하는 가장 우아한 방법은 무엇입니까?

문자열 벡터를 내파하는 가장 우아한 방법은 무엇입니까?

Linda Hamilton
Linda Hamilton원래의
2024-10-24 08:12:02427검색

What is the Most Elegant Way to Implode a Vector of Strings?

문자열 벡터를 우아하게 내포

이 질문은 문자열 벡터를 단일 문자열로 내포하는 가장 정교한 접근 방식을 식별하고자 합니다. 현재 구현된 방법은 다음과 같습니다.

<code class="cpp">static std::string&amp; implode(const std::vector<std::string>&amp; elems, char delim, std::string&amp; s)
{
    for (std::vector<std::string>::const_iterator ii = elems.begin(); ii != elems.end(); ++ii)
    {
        s += (*ii);
        if ( ii + 1 != elems.end() ) {
            s += delim;
        }
    }

    return s;
}

static std::string implode(const std::vector<std::string>&amp; elems, char delim)
{
    std::string s;
    return implode(elems, delim, s);
}</code>

boost::algorithm::join 사용

응답에서는 Boost 라이브러리의 조인 기능을 활용할 것을 제안합니다.

<code class="cpp">#include <boost/algorithm/string/join.hpp>
...
std::string joinedString = boost::algorithm::join(elems, delim);</code>

이 방법은 문자열 파열에 대한 보다 간결하고 효율적인 접근 방식을 제공합니다.

위 내용은 문자열 벡터를 내파하는 가장 우아한 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.