pythonchallenge之C++学习篇-03
提示说一个小写字母两面精确地被大写字母包围,应该指的是周围没有四个而仅仅这两个像这样的:xXXXxXXXx的中间的那个应该是符合条件的
好了标题是re,提示该是使用正则表达式,网页源码里有待处理的字符串,所以这里应该是主要考察的正则表达式
C++里的正则表达式有一些类库类实现,有多个实现,他们是什么以及比较可以参考这里
这里打算采用比较出名的boost,boost好像不止包含正则表达式的功能,具体介绍参照维基
直接包含它显示说没有这个文件,那么它还是需要安装的,我猜安装它可能也需要一些曲折的路线
不管了先找找怎么安装
安装这个有个工具叫做nuget工具,nuget好像只能在windows下使用,但是确实能像pip这类的工具一样使用
执行:
nuget install boost
提示安装成功,不知道安装到哪里了,c盘查找,在xx用户文件里
运行mingw g++编译没通过,说找不到该文件或目录
去boost安装目录,看到build文件夹下有个boost.targets的文件,打开里面显示
<?xml version="1.0" encoding="utf-8"?> <Project ToolVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemDefinitionGroup> <ClCompile> <PreprocessorDefinitions>;%(PreprocessorDefinitions)</PreprocessorDefinitions> <AdditionalIncludeDirectories>$(MSBuildThisFileDirectory)..\..\lib\native\include\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> </ClCompile> </ItemDefinitionGroup> <ItemGroup /> </Project>好像说明了编译器位于哪里。。。好吧改改能不能用?我不知道怎么改,算了
本来想下载一个vs-studio,但是大而且提示我windows版本低,如果vs-studio确实能减少我很多的事情,也可以用用
我把后一个作为比较好的解决方案,但是我现在不打算执行,我想直接用string标准库和char来实现以下这个问题的求解
目的:
文本处理,找到类似于xXXXxXXXx形式的中间的字母
解决方案:
1 # include <iostream> 2 // # include <math.h> 3 # include <string> 4 # include <fstream> 5 # include <cctype> 6 7 8 using std::string; 9 using std::cout; 10 // using std::cin; 11 using std::endl; 12 using namespace std; 13 14 void mathRules(string row) 15 { 16 for (int i = 4; i < row.size() - 4; ++i) 17 { 18 if (islower(row[i]) and isupper(row[i + 1]) and isupper(row[i + 2]) and isupper(row[i + 3]) 19 and isupper(row[i - 1]) and isupper(row[i - 2]) and isupper(row[i - 3]) and islower(row[i + 4]) 20 and islower(row[i - 4]) ) 21 { 22 cout << row[i]; 23 } 24 } 25 } 26 27 28 int main() 29 { 30 ifstream infile;//definate fstream 31 string temp; //define a var to store strings 32 33 infile.open("myfile.txt"); //open the file 34 35 while (getline(infile, temp)) //use getline() fuction 36 { 37 38 mathRules(temp); 39 // cout << temp << endl; 40 } 41 return 0; 42 }
问题以及解决方案:
问题是我没有正则表达式,但是用了cctype也能解决
C++也有丰富的类库不过安装使用扩散的方便性普及性以及程度都不够
使用visualstudio可以减少很多劳动
在C++管理类库的东西是nuget但是只能在win下用,而且要结合vs使用
注:这里之所以没说问题的答案是因为两点:
1.答案隐含在代码里面,如果你能够运行正确代码,那么会得到答案
2.已有同学整理过并发布在网上,不做重复劳动