boost 与 string 的应用
来源:互联网 发布:网络攻防技术技能试卷 编辑:程序博客网 时间:2024/05/17 16:01
// string.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<string>
#include <boost/algorithm/string.hpp>
#include <iostream>
#include <vector>
using namespace std;
using namespace boost;
bool NotH(const char &ch)
{
if(ch == ' ' || ch == 'H' || ch == 'h')
return true;
else
return false;
}
bool is_123digit(const char &ch)
{
if(ch == '1' || ch == '2' || ch == '3')
return true;
else
return false;
}
char ToUpper(char &ch)
{
if(ch <= 'z' && ch >= 'a')
return ch + 'A'-'a';
else
return ch;
}
char Add1(const char &ch)
{
return ch+1;
}
iterator_range<string::iterator> MyFinder1( std::string::iterator begin, std::string::iterator end )
{
std::string::iterator itr;
for(itr = begin; itr!=end; itr++)
{
if((*itr) == '1')
{
std::string::iterator preitr = itr;
iterator_range<string::iterator> ret(preitr, ++itr);
return ret;
}
}
return iterator_range<string::iterator>();
} // boost自己也提供了很多Finder
class SplitNotThisChar
{
public:
SplitNotThisChar(const char ch) : m_char(ch) {}
bool operator ()(const char &ch) const
{
if(ch == m_char)
return true;
else
return false;
}
private:
char m_char;
};
int main()
{
/*
string str1(" hello world! ");
to_upper(str1); // str1 == " HELLO WORLD! "
cout<< "->" << str1 << endl;
string str2(" hello world! ");
string str3 = to_upper_copy(str2);
cout << "->" << str3 << endl << endl;
to_lower(str1);
cout<< "->" << str1 << endl;
string str4 = to_lower_copy(str3);
cout << "->" << str4 <<endl <<endl;
string str5(" aaaaaaa ");
trim_right(str5);
cout << "->" << str5 << endl;
trim_left(str5);
cout << "->" << str5 << endl << endl;
string str6(" hello world! ");
trim_left_if(str6, NotH); // str1 == "ello world! "
cout << "->" << str6 << endl;
string str7(" hello world! ");
string str8;
str8 = trim_left_copy(str7); // str2 == "hello world! "
cout << "->" << str8 << endl;
string str9(" hello world! ");
string stra;
stra = trim_left_copy_if(str9, NotH);
cout << "->" << stra << endl;
*/
/*
// 1 starts_with() 判断一个字符串是否是另外一个字符串的开始串
string str1("hello world!");
string str2("hello");
bool result = starts_with(str1, str2); // result == true
cout << "result --->" << result << endl;
*/
/*
// result 2 istarts_with() 判断一个字符串是否是另外一个字符串的开始串(不区分大小写)
string str1("hello world!");
string str2("Hello");
bool result = istarts_with(str1, str2); // result == true
// cout << "result->" << result << endl;
*/
// 3 ends_with() 判断一个字符串是否是另外一个字符串的结尾串
// 4 iends_with() 判断一个字符串是否是另外一个字符串的结尾串(不区分大小写)
/*
// 5 contains() 判断一个字符串是否包含另外一个字符串
string str1("hello world!");
string str2("llo");
bool result = contains(str1, str2); // result == true
cout << "->" << result << endl;
*/
// icontains() 判断一个字符串是否包含另外一个字符串(不区分大小写)
// equals() 判断两个字符串是否相等
// iequals() 判断两个字符串是否相等(不区分大小写)
// lexicographical_compare() 按照字典排序,如果第一个字符串小于第二个字符串,返回true (我的boost1.33没有实现?)
// ilexicographical_compare() 按照字典排序,如果第一个字符串小于第二个字符串,返回true(不区分大小写)
/*
//all() 判断字符串中的所有字符是否全部满足这个谓词
string str1("12332211");
bool result = all(str1, is_123digit); // result == true
cout << "->" << result << endl;
str1 = "412332211";
result = all(str1, is_123digit); // result == false
cout << "->" << result << endl;
*/
/*
// 1 find_first() 从头查找字符串中的子字符串,返回这个子串在 原串中的iterator_range迭代器
string str1("hello dolly!");
iterator_range<string::iterator> result = find_first(str1,"llo d");
iterator_range<string::iterator>::iterator it = result.begin();
for (; result.end() != it; it ++)
{
cout << (*it) << endl;
}
transform( result.begin(), result.end(), result.begin(), ToUpper ); // str1 == "heLLo dolly!"
cout << "->" << str1 << endl;
*/
// 2 ifind_first() 从头查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器(不区分大小写)
// 3 find_last() 从尾查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器
// 4 ifind_last() 从尾查找字符串中的子字符串,返回这个子串在原串中的iterator_range迭代器(不区分大小写)
/*
// 5 find_nth() 找到第n个匹配的子串(计算从0开始)
string str1("hello dolly!");
iterator_range<string::iterator> result = find_nth(str1,"ll", 1);
transform( result.begin(), result.end(), result.begin(), ToUpper ); // str1 == "hello doLLy!"
cout << "->" << str1 << endl;
*/
//ifind_nth() 找到第n个匹配的子串(计算从0开始)(不区分大小写)
/*
//find_head() 找到字符串的前n个字节
string str1("hello dolly!");
iterator_range<string::iterator> result = find_head(str1,5);
transform( result.begin(), result.end(), result.begin(), ToUpper ); // str1 == "HELLO dolly!"
cout << "->" << str1 << endl;
*/
// find_tail() 找到字符串的后n个字节
/*
//find_token() 找到符合谓词的串
string str1("hello 1 879 world!");
iterator_range<string::iterator> result = find_token(str1,is_123digit);
transform( result.begin(), result.end(), result.begin(), Add1 ); // str1 == "hello 2 world!");
cout << "->" << str1 << endl;
*/
// 10 find_regex() 匹配正则表达式
/*
// 11 find() 使用自己写的查找函数
string str1("hello 1 world!");
iterator_range<string::iterator> result = find(str1,MyFinder1);
transform( result.begin(), result.end(), result.begin(), Add1 ); // str1 == "hello 2 world!");
cout << "->" <<str1 << endl;
*/
/*
//1 replace_first() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串
string str1("hello world!");
replace_first(str1, "hello", "He555llo"); // str1 = "Hello world!"
cout << "->" <<str1 << endl;
*/
/*
//2 replace_first_copy() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串,并且赋值给另一个字符串
string str1("hello world!");
string str2;
str2 = replace_first_copy(str1, "hello", "Hcccello"); // str2 = "Hello world!"
cout << "->" <<str1 << endl;
cout << "->" <<str2 << endl;
*/
// ireplace_first() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串(不区分大小写)
//ireplace_first_copy() 从头找到第一个匹配的字符串,将其替换为给定的另外一个字符串,并且赋值给另一个字符串(不区分大小写)
/*
//erase_first() 从头找到第一个匹配的字符串,将其删除
string str1("hello world!");
erase_first(str1, "llo"); // str1 = "He world!"
cout << "->" <<str1 << endl;
*/
/*
//6 erase_first_copy() 从头找到第一个匹配的字符串,将其删除,并且赋值给另一个字符串
string str1("hello world!");
string str2;
str2 = erase_first_copy(str1, "llo"); // str2 = "He world!"
cout << "->" <<str1 << endl;
cout << "->" <<str2 << endl;
*/
// 7 ierase_first() 从头找到第一个匹配的字符串,将其删除(不区分大小写)
// 8 ierase_first_copy() 从头找到第一个匹配的字符串,将其删除,并且赋值给另一个字符串(不区分大小写)
// // 与上面类似,不过是从字符串尾开始替换
// 9 replace_last()
// 10 replace_last_copy()
// 11 ireplace_last()
// 12 ireplace_last_copy()
// 13 erase_last()
// 14 erase_last_copy()
// 15 ierase_last()
// 16 ierase_last_copy()
/*
// 与上面类似,不过是从字符串n个匹配的开始替换
//17 replace_nth()
string str1("hello world!");
replace_nth(str1, "o", 1, "O"); // str1 = "hello wOrld!"
cout << "->" <<str1 << endl;
*/
// 18 replace_nth_copy()
// 19 ireplace_nth()
// 20 ireplace_nth_copy()
// 21 erase_nth()
// 22 erase_nth_copy()
// 23 ierase_nth()
// 24 ierase_nth_copy()
//
// // 与上面类似,不过是将所有的匹配字符串替换
// 25 replace_all()
// 26 replace_all_copy()
// 27 ireplace_all()
// 28 ireplace_all_copy()
// 29 erase_all()
// 30 erase_all_copy()
// 31 ierase_all()
// 32 ierase_all_copy()
/*
// 33 replace_head() 替换前n个字符
string str1("hello world!");
replace_head(str1, 5, "HELLO"); // str1 = "HELLO world
cout << "->" <<str1 << endl;
*/
/*
//34 replace_head_copy() 替换前n个字符,并且赋值给另一个字符串
string str1("hello world!");
string str2;
str2 = replace_head_copy(str1, 5, "HELLO"); // str2 = "HELLO world!"
cout << "->" <<str1 << endl;
cout << "->" <<str2 << endl;
*/
/*
//35 erase_head() 删除前n个字符
string str1("hello world!");
erase_head(str1, 5); // str1 = " world!"
cout << "->" <<str1 << endl;
*/
/*
//36 erase_head_copy() 删除前n个字符,并且赋值给另一个字符串
string str1("hello world!");
string str2;
str2 = erase_head_copy(str1, 5); // str2 = " world!"
cout << "->" <<str1 << endl;
cout << "->" <<str2 << endl;
*/
// 与上面类似(替换/删除后n个字符)
// 37 replace_tail()
// 38 replace_tail_copy()
// 39 erase_tail()
// 40 erase_tail_copy()
//
// 与正则表示式相关,稍后了解。
// 41 replace_regex()
// 42 replace_regex_copy()
// 43 erase_regex()
// 44 erase_regex_copy()
// 45 replace_all_regex()
// 46 replace_all_regex_copy()
// 47 erase_all_regex()
// 48 erase_all_regex_copy()
//
// 不是很清楚,稍后了解
// 49 find_format()
// 50 find_format_copy()
// 51 find_format_all()
// 52 find_format_all_copy()
/*
//1 find_all() 查找所有匹配的值,并且将这些值放到给定的容器中
string str1("hello world!");
std::vector<std::string> result;
find_all(result, str1, "l"); // result = [3]("l","l","l")
cout << "->" <<str1 << endl;
cout << result.size() << endl;
*/
// 2 ifind_all() 查找所有匹配的值,并且将这些值放到给定的容器中(不区分大小写)
// 3 find_all_regex() 与正则表达式相关,稍后了解
/*
// 4 split() 按照给定的谓词切割字符串,并且把切割后的值放入到给定的容器中
string str1("hello world!");
std::vector<std::string> result;
split(result, str1, SplitNotThisChar('l')); // result = [4]("he", "", "o wor", "d!")
for(int i= 0; i < result.size(); ++i)
{
cout << result[i] << endl;
}
*/
/*
//5 split_regex() 与正则表达式相关,稍后了解
//6 iter_find() 按照给定Finder查找字符串,并且把查找到的值放入到给定的容器中
string str1("hello world!");
std::vector<std::string> result;
// first_finder为Boost自带的Finder
iter_find(result, str1, first_finder("l")); // result = [3]("l","l","l")
for(int i= 0; i < result.size(); ++i)
{
cout << result[i] << endl;
}
*/
//7 iter_split() 按照给定的Finder切割字符串,并且把切割后的值放入到给定的容器中
string str1("hello world!");
std::vector<std::string> result;
iter_split(result, str1, first_finder("l")); // result = [4]("he","","o wor","d!")
for(int i= 0; i < result.size(); ++i)
{
cout << result[i] << endl;
}
int a;
cin >>a;
return 0;
}
- boost 与 string 的应用
- StringBuffer与String的区别与应用
- Boost.log的应用
- Boost.log的应用
- Boost.log的应用
- boost string库的使用
- String,StringBuffer,StringBuilder的区分与应用
- BOOST::String
- boost string
- boost string
- boost::string
- boost::thread的简单应用
- boost 中 thread 的应用
- boost中信号的应用
- boost学习与应用笔记之一:Boost.Atomic
- boost::any 与 boost::variant 的区别
- boost::any 与 boost::variant 的区别
- boost::function与boost::bind 的使用
- Scala函数解析
- Java中的内部类
- Github简单教程--入门
- hybrid app
- poj1651 Multiplication Puzzle(区间DP)
- boost 与 string 的应用
- Android Data Binding框架指南
- lintcode:Subsets II
- 面向协议编程
- 从此不求人:自主研发一套PHP前端开发框架(9)
- 【HowTo ML】分类问题->逻辑回归
- 【Android】Eclipse自动编译NDK/JNI的三种方法
- ACM ProblemR
- C++学习笔记——第12章 继承