- . , , - , , .
std::string, std::vector, , begin() end() ( ).
std::string , , , .
#include <iostream>
#include <string>
#include <iterator>
template< typename Range, typename OutputIterator >
OutputIterator copy_escaped( const Range& in, OutputIterator out ){
for( const auto& c : in ){
switch( c ){
case '"':
*out++ = '\\';
*out++ = '"';
break;
case '\\':
*out++ = '\\';
*out++ = '\\';
break;
case '\n':
*out++ = '\\';
*out++ = 'n';
break;
case '\r':
*out++ = '\\';
*out++ = 'r';
break;
case '\t':
*out++ = '\\';
*out++ = 't';
break;
default:
*out++ = c;
}
}
return out;
}
, .
:
int main()
{
std::string s = "abc\"def\\hhh\"i";
std::cout << s << std::endl;
copy_escaped( s, std::ostream_iterator<char>( std::cout ) );
std::cout << std::endl;
std::string escaped_s;
escaped_s.reserve( s.size() );
copy_escaped( s, back_inserter( escaped_s ) );
std::cout << escaped_s << std::endl;
}
.
source
share