
一道关于删除文本中重复内容的编程题,求教c/c++大神!!!
void SplitStringByMark(const std::string& strString, char cMark, std::vector<std::string>& vecString)
{
std::string strStrIn = strString;
for (int nPos = 0; nPos < (int)strStrIn.size(); nPos++)
{
unsigned char ch = strStrIn[nPos];
if (ch > 0x80) //汉字
{
nPos++;
continue;
}
if (cMark == strStrIn[nPos])
{
std::string strDiv = strStrIn.substr(0, nPos);
strStrIn = strStrIn.substr(nPos+1, strStrIn.size() - nPos - 1);
vecString.push_back(strDiv);
nPos = -1;
}
}
if (!strStrIn.empty())
{
vecString.push_back(strStrIn);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
string strSource = "ab=123|bc=234|cd=345|dc=456";
vector<string> vecSplit;
SplitStringByMark(strSource, '|', vecSplit);
map<string, string> mapString;
vector<string>::iterator iterStr = vecSplit.begin();
for (; iterStr != vecSplit.end(); ++iterStr)
{
vector<string> vecStrInfo;
SplitStringByMark(*iterStr, '=', vecStrInfo);
if (2 != vecStrInfo.size())
{
cout<<"源字符串内容错误!";
break;
}
mapString.insert(pair<string, string>(vecStrInfo.at(0), vecStrInfo.at(1)));
}
string strIn = "";
cout<<"请输入要查找的字符串:"<<endl;
while(cin>>strIn)
{
if (mapString.count(strIn))
{
cout<<strIn<<"对应的值是:"<<mapString.at(strIn)<<endl;
}
else
{
cout<<"不存在"<<strIn<<"对应的值。"<<endl;
cout<<"要退出程序请输入:y,不退出请输入N"<<endl;
cin>>strIn;
if (strIn == "y")
{
break;
}
cout<<"请输入要查找的字符串:"<<endl;
}
}
system("PAUSE");
return 0;
}
第二个问题:我有一个十分垃圾的办法,只能用于你这种文本内容十分少的情况,多了就不行了。我也不了解真正的文本编译器是如何实现的,如果你找到了资料希望能给我分享一下。
#include <algorithm>
#define MAXBUFERSIZE 1024
int _tmain(int argc, _TCHAR* argv[])
{
ifstream inStream;
inStream.open("D:\\123.txt");
if (!inStream.is_open())
{
cout<<"未打开文件"<<endl;
exit(1);
}
vector<string> vecLineInfo;
char buffer[MAXBUFERSIZE];
while(inStream.getline(buffer, MAXBUFERSIZE))
{
vecLineInfo.push_back((string)buffer);
}
inStream.close();
sort(vecLineInfo.begin(), vecLineInfo.end());
vecLineInfo.erase(unique(vecLineInfo.begin(), vecLineInfo.end()), vecLineInfo.end());
ofstream outStream;
outStream.open("D:\\123.txt");
for (unsigned int i = 0; i < vecLineInfo.size(); ++i)
{
outStream<<vecLineInfo.at(i)<<endl;
}
outStream.close();
return 0;
}
伪代码如下:
题1
vecBefStr;
vecAftStr;
strTemp;
read (char in string)
{
switch(char)
{ case '=':
{
vecBefStr.push(strTemp);
strTemp = "";
}
break;
case '|':
{
vecAftStr.push(strTemp);
strTemp = "";
}
break;
default:
strTemp += char;
break;
}
}
至于查找, 遍历2个数组匹配, 找到对应序号的字符串就OK了.
题2:
vecLine;
readline(line1);
if line1 not in vecLine
vecLine.push(line1);
-.- 这题不要太简单啊... 判断是否在数组里, 直接遍历数组就是...
2014-10-22
#include "stdafx.h"
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <cstdlib>
int main(int argc, char* argv[])
{
ifstream ifile("E:\\temp.txt");
vector<string> vec;
string ss;
while(ifile>>ss) vec.push_back(ss);
ifile.close();
vector<string> vecWritedata;
for(int i = 0; i < vec.size(); ++i)
{
if (0 == i)
{
vecWritedata.push_back(vec[i]);
}
else
{
bool bexist = false;
for (int j = 0; j < vecWritedata.size(); ++j)
{
if (vec[i] == vecWritedata[j])
{
bexist = true;
break;
}
}
if (!bexist)
vecWritedata.push_back(vec[i]);
}
}
ofstream ofile("E:\\temp.txt");
vector<string>::iterator it;
for(it = vecWritedata.begin(); it<vecWritedata.end(); it++)
ofile<<*it<<endl;
ofile.close();
system("PAUSE");
return 0;
}
题2:先把字符串都存入数组,再索引对比删除
大神,有没有具体点代买的实现,挺着急的。。会继续追加财富值的;
大神要具体代码呀,跪求啊,江湖救急啊!!!
c++处理字符串真的很头疼,用其它语言行不行啊