string.cpp,RUN
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <map>
#include <cstring>
//////////////////////////////////////////////////////////////////////////////////////////////////
//输入数值和字符(不带空格)
void InputNumberAndChar()
{
//year-month-day
int year, month, day;
char temp1, temp2;
while (std::cin >> year >> temp1 >> month >> temp2 >> day)
{//Ctrl+Break will break the loop
std::cout << year << temp1 << month << temp2 << day << std::endl;
}
}
//输入带空格的字符串
void InputStringWithBlanks()
{
//http://www.cplusplus.com/reference/string/string/getline/
std::string str;
std::getline(std::cin, str); //std::getline(std::cin, str, '\n');遇到'\n停止读入数据
char temp[256];
std::cin.getline(temp, sizeof(temp));
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//基本的字符串用法
void BasicStringFunction()
{
//convert char array to string
char cMsg[] = "Hello world.";
std::string str(cMsg);
//convert a number to a string
int n = 10;
str = std::to_string(n);
//convert a string to a number
int m = std::stoi(str);
str = str + " add"; //operator +
if (str == "str")
{//operator compare
}
char temp[256];
strcpy_s(temp, str.c_str()); //convert a string to a char array
std::cout << temp << std::endl;
{//从字符串中输入
std::string content = "ABS 10 12.3";
std::stringstream ss(content);
std::string v0;
int v1;
float v2;
ss >> v0 >> v1 >> v2;
std::cout << v0 << "\t" << v1 << "\t" << v2 << std::endl;
}
}
//http://www.cplusplus.com/reference/string/string/find/
//查找指定的字符串str(完全包含)
int PatternNumber(const std::string& content, const std::string& str)
{
int count = 0;
size_t pos = content.find(str);
while (pos != std::string::npos)
{
count++;
pos = content.find(str, pos + 1);
}
return count;
}
//http://www.cplusplus.com/reference/string/string/find_first_of/
//查找字符串中,包含模式str的次数(出现str包含的任一字符)
int PatternNumber_OneOf(const std::string& content, const std::string& str)
{
int count = 0;
size_t pos = content.find_first_of(str);
while (pos != std::string::npos)
{
count++;
pos = content.find_first_of(str, pos + 1);
}
return count;
}
//把输入content里的符号换成空格
void ReplaceSymbolWithBlank(std::string& content)
{
//http://www.cplusplus.com/reference/string/string/find_first_of/
const std::string Pattern = ".,\"!?-"; //句子中使用的符号
std::size_t found = content.find_first_of(Pattern);
while (found != std::string::npos)
{
content[found] = ' ';
found = content.find_first_of(Pattern, found + 1);
}
}
//利用std::stringstream统计句子中单词个数,单词之间用 空格 \t \n 分离
//输入字符串不包含无关的符号,如.,"?!等
void ParseContent(const std::string& content, std::vector<std::string>& vWords)
{
std::stringstream ss(content);
std::string word;
while (ss >> word) //标准的流输入
{
vWords.push_back(word);
}
}
//https://en.cppreference.com/w/cpp/container/map
//统计词频
//输入:vWords,输出:msu,map映射
void WordFrequency(const std::vector<std::string>& vWords, std::map<std::string, unsigned int>& msu)
{
for (auto i = 0; i < vWords.size(); i++)
{
//https://en.cppreference.com/w/cpp/container/map/find
//在msu中查找key为vWords[i]的项
if (msu.find(vWords[i]) == msu.end())//msu中没有word
{ //不存在,添加一项
//https://en.cppreference.com/w/cpp/container/map/operator_at
msu[vWords[i]] = 1;
}
else
{ //找到,直接使用
//https://en.cppreference.com/w/cpp/container/map/operator_at
msu[vWords[i]]++;
}
}
}
//显示词频信息
void Show(const std::map<std::string, unsigned int>& msu)
{
//访问std::map结构
//https://en.cppreference.com/w/cpp/container/map/begin
for (auto itr = msu.begin(); itr != msu.end(); itr++)
{
std::cout << itr->first << ": " << itr->second << std::endl;
}
}
//提取句子中包含的单词,以及其它信息
void ParseContent(const std::string& content)
{
std::string temp = content;
ReplaceSymbolWithBlank(temp); //把temp中的.,"?!等符号替换成空格
std::vector<std::string> vWords;
ParseContent(temp, vWords); //根据空格分割信息提取单词
//std::map结构可以用来存储词频
//https://en.cppreference.com/w/cpp/container/map
std::map<std::string, unsigned int> msu;
WordFrequency(vWords, msu);
Show(msu); //显示词频信息
auto nWords = vWords.size();
const std::string Pattern = ".,\"!?-"; //字符串处理示例时使用,句子中使用的符号
auto nSymbols = PatternNumber_OneOf(content, Pattern);//统计包含模式的次数
}
int main()
{
std::string content1;
std::string content2;
content1 = "Although the principles of structured programming improved the clarity, reliability, and ease of maintenance of programs, large - scale programming still remains a challenge.OOP \
brings a new approach to that challenge.";
content2 = "Unlike procedural programming, which emphasizes \
algorithms, OOP emphasizes the data.Rather than try to fit a problem to the procedural \
approach of a language, OOP attempts to fit the language to the problem.";
std::string content = content1 + content2;
ParseContent(content);
//InputNumberAndChar();
//InputStringWithBlanks();
BasicStringFunction();
return 0;
}