mirror of
https://github.com/boostorg/algorithm.git
synced 2025-07-29 12:07:18 +02:00
String Algorithm Library: initial commit
[SVN r22431]
This commit is contained in:
75
string/example/Jamfile
Normal file
75
string/example/Jamfile
Normal file
@ -0,0 +1,75 @@
|
||||
# Boost string_algo library examples Jamfile ---------------------------------
|
||||
#
|
||||
# Copyright Pavol Droba 2002-2003. Use, modification and
|
||||
# distribution is subject to the Boost Software License, Version
|
||||
# 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
# http://www.boost.org/LICENSE_1_0.txt)
|
||||
#
|
||||
# See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
subproject libs/algorithm/string/example ;
|
||||
|
||||
exe conv_example
|
||||
:
|
||||
conv_example.cpp
|
||||
:
|
||||
<include>$(BOOST_ROOT)
|
||||
:
|
||||
;
|
||||
|
||||
exe predicate_example
|
||||
:
|
||||
predicate_example.cpp
|
||||
:
|
||||
<include>$(BOOST_ROOT)
|
||||
:
|
||||
;
|
||||
|
||||
exe find_example
|
||||
:
|
||||
find_example.cpp
|
||||
:
|
||||
<include>$(BOOST_ROOT)
|
||||
:
|
||||
;
|
||||
|
||||
exe replace_example
|
||||
:
|
||||
replace_example.cpp
|
||||
:
|
||||
<include>$(BOOST_ROOT)
|
||||
:
|
||||
;
|
||||
|
||||
exe rle_example
|
||||
:
|
||||
rle_example.cpp
|
||||
:
|
||||
<include>$(BOOST_ROOT)
|
||||
:
|
||||
;
|
||||
|
||||
exe trim_example
|
||||
:
|
||||
trim_example.cpp
|
||||
:
|
||||
<include>$(BOOST_ROOT)
|
||||
:
|
||||
;
|
||||
|
||||
exe regex_example
|
||||
:
|
||||
regex_example.cpp
|
||||
<lib>../../../regex/build/boost_regex
|
||||
:
|
||||
<include>$(BOOST_ROOT)
|
||||
:
|
||||
;
|
||||
|
||||
exe split_example
|
||||
:
|
||||
split_example.cpp
|
||||
:
|
||||
<include>$(BOOST_ROOT)
|
||||
:
|
||||
;
|
41
string/example/conv_example.cpp
Normal file
41
string/example/conv_example.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Boost string_algo library example file ---------------------------------//
|
||||
|
||||
// Copyright Pavol Droba 2002-2003. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "* Case Conversion Example *" << endl << endl;
|
||||
|
||||
string str1("AbCdEfG");
|
||||
vector<char> vec1( str1.begin(), str1.end() );
|
||||
|
||||
// Convert vector of chars to lower case
|
||||
cout << "lower-cased copy of vec1: ";
|
||||
to_lower_copy( ostream_iterator<char>(cout), vec1 );
|
||||
cout << endl;
|
||||
|
||||
// Conver string str1 to upper case ( copy the input )
|
||||
cout << "upper-cased copy of str1: " << to_upper_copy( str1 ) << endl;
|
||||
|
||||
// Inplace conversion
|
||||
to_lower( str1 );
|
||||
cout << "lower-cased str1: " << str1 << endl;
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
58
string/example/find_example.cpp
Normal file
58
string/example/find_example.cpp
Normal file
@ -0,0 +1,58 @@
|
||||
// Boost string_algo library example file ---------------------------------//
|
||||
|
||||
// Copyright Pavol Droba 2002-2003. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
#include <boost/algorithm/string/find.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "* Find Example *" << endl << endl;
|
||||
|
||||
string str1("abc___cde___efg");
|
||||
string str2("abc");
|
||||
|
||||
// find "cde" substring
|
||||
iterator_range<string::iterator> range=find_first( str1, string("cde") );
|
||||
|
||||
// convert a substring to upper case
|
||||
// note that iterator range can be directly passed to the algorithm
|
||||
to_upper( range );
|
||||
|
||||
cout << "str1 with upper-cased part matching cde: " << str1 << endl;
|
||||
|
||||
// get a head of the string
|
||||
iterator_range<string::iterator> head=find_head( str1, 3 );
|
||||
cout << "head(3) of the str1: " << string( head.begin(), head.end() ) << endl;
|
||||
|
||||
// get the tail
|
||||
head=find_tail( str2, 5 );
|
||||
cout << "tail(5) of the str2: " << string( head.begin(), head.end() ) << endl;
|
||||
|
||||
// char processing
|
||||
char text[]="hello dolly!";
|
||||
iterator_range<char*> crange=find_last(text,"ll");
|
||||
|
||||
// transform the range ( add 1 )
|
||||
transform( crange.begin(), crange.end(), crange.begin(), bind2nd( plus<char>(), 1 ) );
|
||||
// uppercase the range
|
||||
to_upper( crange );
|
||||
|
||||
cout << text << endl;
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
61
string/example/predicate_example.cpp
Normal file
61
string/example/predicate_example.cpp
Normal file
@ -0,0 +1,61 @@
|
||||
// Boost string_algo library example file ---------------------------------//
|
||||
|
||||
// Copyright Pavol Droba 2002-2003. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <boost/algorithm/string/predicate.hpp>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/bind.hpp>
|
||||
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "* Predicate Example *" << endl << endl;
|
||||
|
||||
string str1("123xxx321");
|
||||
string str2("abc");
|
||||
|
||||
// Check if str1 starts with '123'
|
||||
cout << "str1 starts with \"123\": " <<
|
||||
(starts_with( str1, string("123") )?"true":"false") << endl;
|
||||
|
||||
// Check if str1 ends with '123'
|
||||
cout << "str1 ends with \"123\": " <<
|
||||
(ends_with( str1, string("123") )?"true":"false") << endl;
|
||||
|
||||
// Check if str1 containes 'xxx'
|
||||
cout << "str1 contains \"xxx\": " <<
|
||||
(contains( str1, string("xxx") )?"true":"false") << endl;
|
||||
|
||||
|
||||
// Check if str2 equals to 'abc'
|
||||
cout << "str2 equals \"abc\": " <<
|
||||
(equals( str2, string("abc") )?"true":"false") << endl;
|
||||
|
||||
|
||||
// Classification functors and all predicate
|
||||
if ( all(";.,", is_punct() ) )
|
||||
{
|
||||
cout << "\";.,\" are all punctuation characters" << endl;
|
||||
}
|
||||
|
||||
// Classification predicated can be combined
|
||||
if ( all("abcxxx", is_any_of("xabc") && !is_space() ) )
|
||||
{
|
||||
cout << "true" << endl;
|
||||
}
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
42
string/example/regex_example.cpp
Normal file
42
string/example/regex_example.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
// Boost string_algo library example file ---------------------------------//
|
||||
|
||||
// Copyright Pavol Droba 2002-2003. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <boost/regex.hpp>
|
||||
#include <boost/algorithm/string/regex.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "* Regex Example *" << endl << endl;
|
||||
|
||||
string str1("abc__(456)__123__(123)__cde");
|
||||
|
||||
// Replace all substrings matching (digit+)
|
||||
cout <<
|
||||
"replace all (digit+) in str1 with #digit+# :" <<
|
||||
replace_all_regex_copy( str1, regex("\\(([0-9]+)\\)"), string("#$1#") ) << endl;
|
||||
|
||||
// Erase all substrings matching (digit+)
|
||||
cout <<
|
||||
"remove all sequences of letters from str1 :" <<
|
||||
erase_all_regex_copy( str1, regex("[[:alpha:]]+") ) << endl;
|
||||
|
||||
// in-place regex transformation
|
||||
replace_all_regex( str1, regex("_(\\([^\\)]*\\))_"), string("-$1-") );
|
||||
cout << "transformad str1: " << str1 << endl;
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
88
string/example/replace_example.cpp
Normal file
88
string/example/replace_example.cpp
Normal file
@ -0,0 +1,88 @@
|
||||
// Boost string_algo library example file ---------------------------------//
|
||||
|
||||
// Copyright Pavol Droba 2002-2003. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <boost/algorithm/string/replace.hpp>
|
||||
#include <boost/algorithm/string/erase.hpp>
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
|
||||
//Following two includes contain second-layer function.
|
||||
//They are already included by first-layer header
|
||||
|
||||
//#include <boost/algorithm/string/replace2.hpp>
|
||||
//#include <boost/algorithm/string/find2.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
// uppercase formatter
|
||||
/*
|
||||
Convert an input to upper case.
|
||||
Note, that this formatter can be used only on std::string inputs.
|
||||
*/
|
||||
inline string upcase_formatter(
|
||||
const iterator_range<string::const_iterator>& Replace )
|
||||
{
|
||||
string Temp(Replace.begin(), Replace.end());
|
||||
to_upper(Temp);
|
||||
return Temp;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "* Replace Example *" << endl << endl;
|
||||
|
||||
string str1("abc___cde___efg");
|
||||
|
||||
// Erase 6-9th characters from the string
|
||||
cout << "str1 without 6th to 9th character:" <<
|
||||
erase_range_copy( str1, make_iterator_range(str1.begin()+6, str1.begin()+9) ) << endl;
|
||||
|
||||
// Replace 6-9th character with '+++'
|
||||
cout << "str1 with 6th to 9th character replaced with '+++': " <<
|
||||
replace_range_copy(
|
||||
str1, make_iterator_range(str1.begin()+6, str1.begin()+9), "+++" ) << endl;
|
||||
|
||||
cout << "str1 with 'cde' replaced with 'XYZ': ";
|
||||
|
||||
// Replace first 'cde' with 'XYZ'. Modify the input
|
||||
replace_first_copy( ostream_iterator<char>(cout), str1, "cde", "XYZ" );
|
||||
cout << endl;
|
||||
|
||||
// Replace all '___'
|
||||
cout << "str1 with all '___' replaced with '---': " <<
|
||||
replace_all_copy( str1, "___", "---" ) << endl;
|
||||
|
||||
// Erase all '___'
|
||||
cout << "str1 without all '___': " <<
|
||||
erase_all_copy( str1, "___" ) << endl;
|
||||
|
||||
// replace third and 5th occurrence of _ in str1
|
||||
// note that nth argument is 0-based
|
||||
replace_nth( str1, "_", 4, "+" );
|
||||
replace_nth( str1, "_", 2, "+" );
|
||||
|
||||
cout << "str1 with third and 5th occurrence of _ replace: " << str1 << endl;
|
||||
|
||||
// Custom formatter examples
|
||||
string str2("abC-xxxx-AbC-xxxx-abc");
|
||||
|
||||
// Find string 'abc' ignoring the case and convert it to upper case
|
||||
cout << "Upcase all 'abc'(s) in the str2: " <<
|
||||
find_format_all_copy(
|
||||
str2,
|
||||
first_finder("abc", is_iequal()),
|
||||
upcase_formatter );
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
241
string/example/rle_example.cpp
Normal file
241
string/example/rle_example.cpp
Normal file
@ -0,0 +1,241 @@
|
||||
// Boost string_algo library example file ---------------------------------//
|
||||
|
||||
// Copyright Pavol Droba 2002-2003. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
/*
|
||||
RLE compression using replace framework. Goal is to compress a sequence of
|
||||
repeating characters into 3 bytes ( repeat mark, character and repetition count ).
|
||||
For simplification, it works only on numeric-value sequences.
|
||||
*/
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <boost/detail/iterator.hpp>
|
||||
#include <boost/algorithm/string/find_format.hpp>
|
||||
#include <boost/algorithm/string/finder.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
// replace mark specification, specialize for a specific element type
|
||||
template< typename T > T repeat_mark() { return std::numeric_limits<T>::max(); };
|
||||
|
||||
// Compression -----------------------------------------------------------------------
|
||||
|
||||
|
||||
// compress finder -rle
|
||||
/*
|
||||
Find a sequence which can be compressed. It has to be at least 3-character long
|
||||
sequence of repetitive characters
|
||||
*/
|
||||
struct find_compressF
|
||||
{
|
||||
// Construction
|
||||
find_compressF() {}
|
||||
|
||||
// Operation
|
||||
template<typename ForwardIteratorT>
|
||||
iterator_range<ForwardIteratorT> operator()(
|
||||
ForwardIteratorT Begin,
|
||||
ForwardIteratorT End ) const
|
||||
{
|
||||
typedef ForwardIteratorT input_iterator_type;
|
||||
typedef typename boost::detail::iterator_traits<input_iterator_type>::value_type value_type;
|
||||
typedef iterator_range<input_iterator_type> result_type;
|
||||
|
||||
// begin of the matching segment
|
||||
input_iterator_type MStart=End;
|
||||
// Repetition counter
|
||||
value_type Cnt=0;
|
||||
|
||||
// Search for a sequence of repetitive characters
|
||||
for(input_iterator_type It=Begin; It!=End;)
|
||||
{
|
||||
input_iterator_type It2=It++;
|
||||
|
||||
if ( It==End || Cnt>=std::numeric_limits<value_type>::max() )
|
||||
{
|
||||
return result_type( MStart, It );
|
||||
}
|
||||
|
||||
if ( *It==*It2 )
|
||||
{
|
||||
if ( MStart==End )
|
||||
{
|
||||
// Mark the start
|
||||
MStart=It2;
|
||||
}
|
||||
|
||||
// Increate repetition counter
|
||||
Cnt++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ( MStart!=End )
|
||||
{
|
||||
if ( Cnt>2 )
|
||||
return result_type( MStart, It );
|
||||
else
|
||||
{
|
||||
MStart=End;
|
||||
Cnt=0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result_type( End, End );
|
||||
}
|
||||
};
|
||||
|
||||
// rle compress format
|
||||
/*
|
||||
Transform a sequence into repeat mark, character and count
|
||||
*/
|
||||
template<typename SeqT>
|
||||
struct format_compressF
|
||||
{
|
||||
private:
|
||||
typedef SeqT result_type;
|
||||
typedef typename SeqT::value_type value_type;
|
||||
|
||||
public:
|
||||
// Construction
|
||||
format_compressF() {};
|
||||
|
||||
// Operation
|
||||
template< typename ReplaceT >
|
||||
result_type operator()( const ReplaceT& Replace ) const
|
||||
{
|
||||
SeqT r;
|
||||
r.push_back( repeat_mark<value_type>() );
|
||||
r.push_back( *(Replace.begin()) );
|
||||
r.push_back( value_type( Replace.size() ) );
|
||||
|
||||
return r;
|
||||
}
|
||||
};
|
||||
|
||||
// Decompression -----------------------------------------------------------------------
|
||||
|
||||
|
||||
// find decompress-rle functor
|
||||
/*
|
||||
find a repetition block
|
||||
*/
|
||||
struct find_decompressF
|
||||
{
|
||||
// Construction
|
||||
find_decompressF() {}
|
||||
|
||||
// Operation
|
||||
template<typename ForwardIteratorT>
|
||||
iterator_range<ForwardIteratorT> operator()(
|
||||
ForwardIteratorT Begin,
|
||||
ForwardIteratorT End ) const
|
||||
{
|
||||
typedef ForwardIteratorT input_iterator_type;
|
||||
typedef typename boost::detail::iterator_traits<input_iterator_type>::value_type value_type;
|
||||
typedef iterator_range<input_iterator_type> result_type;
|
||||
|
||||
for(input_iterator_type It=Begin; It!=End; It++)
|
||||
{
|
||||
if( *It==repeat_mark<value_type>() )
|
||||
{
|
||||
// Repeat mark found, extract body
|
||||
input_iterator_type It2=It++;
|
||||
|
||||
if ( It==End ) break;
|
||||
It++;
|
||||
if ( It==End ) break;
|
||||
It++;
|
||||
|
||||
return result_type( It2, It );
|
||||
}
|
||||
}
|
||||
|
||||
return result_type( End, End );
|
||||
}
|
||||
};
|
||||
|
||||
// rle decompress format
|
||||
/*
|
||||
transform a repetition block into a sequence of characters
|
||||
*/
|
||||
template< typename SeqT >
|
||||
struct format_decompressF
|
||||
{
|
||||
private:
|
||||
typedef SeqT result_type;
|
||||
typedef typename SeqT::value_type value_type;
|
||||
|
||||
public:
|
||||
// Construction
|
||||
format_decompressF() {};
|
||||
|
||||
// Operation
|
||||
template< typename ReplaceT >
|
||||
result_type operator()( const ReplaceT& Replace ) const
|
||||
{
|
||||
// extract info
|
||||
typename ReplaceT::const_iterator It=Replace.begin();
|
||||
|
||||
value_type Value=*(++It);
|
||||
value_type Repeat=*(++It);
|
||||
|
||||
SeqT r;
|
||||
for( value_type Index=0; Index<Repeat; Index++ ) r.push_back( Value );
|
||||
|
||||
return r;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "* RLE Compression Example *" << endl << endl;
|
||||
|
||||
string original("123_AA_*ZZZZZZZZZZZZZZ*34");
|
||||
|
||||
// copy compression
|
||||
string compress=find_format_all_copy(
|
||||
original,
|
||||
find_compressF(),
|
||||
format_compressF<string>() );
|
||||
|
||||
cout << "Compressed string: " << compress << endl;
|
||||
|
||||
// Copy decompression
|
||||
string decompress=find_format_all_copy(
|
||||
compress,
|
||||
find_decompressF(),
|
||||
format_decompressF<string>() );
|
||||
|
||||
cout << "Decompressed string: " << decompress << endl;
|
||||
|
||||
// in-place compression
|
||||
find_format_all(
|
||||
original,
|
||||
find_compressF(),
|
||||
format_compressF<string>() );
|
||||
|
||||
cout << "Compressed string: " << original << endl;
|
||||
|
||||
// in-place decompression
|
||||
find_format_all(
|
||||
original,
|
||||
find_decompressF(),
|
||||
format_decompressF<string>() );
|
||||
|
||||
cout << "Decompressed string: " << original << endl;
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
63
string/example/split_example.cpp
Normal file
63
string/example/split_example.cpp
Normal file
@ -0,0 +1,63 @@
|
||||
// Boost string_algo library example file ---------------------------------//
|
||||
|
||||
// Copyright Pavol Droba 2002-2003. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <functional>
|
||||
#include <boost/algorithm/string/iterator_range.hpp>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
#include <boost/algorithm/string/split.hpp>
|
||||
#include <boost/algorithm/string/find_iterator.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "* Split Example *" << endl << endl;
|
||||
|
||||
string str1("abc-*-ABC-*-aBc");
|
||||
|
||||
cout << "Before: " << str1 << endl;
|
||||
|
||||
// Find all 'abc' substrings (ignoring the case)
|
||||
// Create a find_iterator
|
||||
typedef find_iterator<string::iterator> string_find_iterator;
|
||||
for(string_find_iterator It=
|
||||
make_find_iterator(str1, first_finder("abc", is_iequal()));
|
||||
It!=string_find_iterator();
|
||||
++It)
|
||||
{
|
||||
cout << copy_iterator_range<std::string>(*It) << endl;
|
||||
// shift all chars in the match by one
|
||||
transform(
|
||||
It->begin(), It->end(),
|
||||
It->begin(),
|
||||
bind2nd( plus<char>(), 1 ) );
|
||||
}
|
||||
|
||||
// Print the string now
|
||||
cout << "After: " << str1 << endl;
|
||||
|
||||
// Split the string into tokens ( use '-' and '*' as delimiters )
|
||||
// We need copies of the input only, and adjacent tokens are compressed
|
||||
vector<std::string> ResultCopy;
|
||||
split(ResultCopy, str1, is_any_of("-*"), token_compress_on);
|
||||
|
||||
for(unsigned int nIndex=0; nIndex<ResultCopy.size(); nIndex++)
|
||||
{
|
||||
cout << nIndex << ":" << ResultCopy[nIndex] << endl;
|
||||
};
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
47
string/example/trim_example.cpp
Normal file
47
string/example/trim_example.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
// Boost string_algo library example file ---------------------------------//
|
||||
|
||||
// Copyright Pavol Droba 2002-2003. Use, modification and
|
||||
// distribution is subject to the Boost Software License, Version
|
||||
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
||||
// http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
// See http://www.boost.org for updates, documentation, and revision history.
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <boost/algorithm/string/trim.hpp>
|
||||
#include <boost/algorithm/string/classification.hpp>
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "* Trim Example *" << endl << endl;
|
||||
|
||||
string str1(" 1x x x x1 ");
|
||||
string str2("<>trim<>");
|
||||
string str3("123abs343");
|
||||
|
||||
// Simple left trim
|
||||
cout << "trim_left copy of str1: " << "\"" << trim_left_copy( str1 ) << "\"" << endl;
|
||||
|
||||
// Inplace right trim
|
||||
trim_right( str1 );
|
||||
cout << "trim_right on str1: " << "\"" << str1 << "\"" << endl;
|
||||
|
||||
// Parametric trim. 'Space' is defined using is_any_of predicate
|
||||
cout
|
||||
<< "trimmed copy of str4 ( space='<>' ): "
|
||||
<< "\""<< trim_copy_if( str2, is_any_of("<>") ) << "\"" << endl;
|
||||
|
||||
|
||||
// Parametric trim. 'Space' is defined using is_digit predicate
|
||||
cout
|
||||
<< "trimmed copy of str5 ( space=digit ): "
|
||||
<< "\"" << trim_copy_if( str3, is_digit() ) << "\"" << endl;
|
||||
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user