33,311
社区成员
发帖
与我相关
我的任务
分享
// inserting into a string
#include <iostream>
#include <string>
int main ()
{
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
std::cout << str << '\n';
return 0;
}
哪里需要循环[/quote]
首先谢谢你的耐心解答,insert的用法我看了。但是这些用法我不都没想到怎么去实现目前我想要的功能。我在描述下我想要的功能。
我有一个字符串vector里面包含已有的关键词,关键词数目不定,如“王菲”、“陈奕迅”等,有另外一个需要标注的字符串,如“陈奕迅和王菲合唱的歌曲”,我需要把男女歌手分别标出来,男用#陈奕迅#,女用*王菲*,就是从“陈奕迅和王菲合唱的歌曲”到“#陈奕迅#和*王菲*合唱的歌曲”这样一个简单的转化。如果要标注,自然要一个个标,vector[0]先比较,然后得到以开头为基准的相对位置,然后insert一个#或者*,然后再是vector[1],...这样写一个循环,是我原先的构想。#include<iostream>
#include<io.h>
#include<string>
using namespace std;
void scanFileDepth(string fileName,int &numFile,int depth=1)
{
const char* CfileName=fileName.c_str();
_finddata_t fileContent;
long handle;
if((handle=_findfirst(CfileName,&fileContent))!=-1)
{
if(strcmp(fileContent.name,"..")!=0&&strcmp(fileContent.name,".")!=0)
{
if(fileContent.attrib==_A_SUBDIR)
{
for(int i=0;i<depth;i++)
{
cout<<"-----";
}
}
else
{
for(int i=0;i<depth;i++)
{
cout<<" ";
}
numFile++;
}
cout<<fileContent.name<<endl;
}
//system("pause");
if(fileContent.attrib==_A_SUBDIR&&strcmp(fileContent.name,"..")!=0&&strcmp(fileContent.name,".")!=0)
{
chdir(fileContent.name);
scanFileDepth(fileName,numFile,depth+1);
}
while(_findnext(handle,&fileContent)==0)
{
if(strcmp(fileContent.name,"..")!=0&&strcmp(fileContent.name,".")!=0)
{
if(fileContent.attrib==_A_SUBDIR)
{
for(int i=0;i<depth;i++)
{
cout<<"-----";
}
}
else
{
for(int i=0;i<depth;i++)
{
cout<<" ";
}
numFile++;
}
cout<<fileContent.name<<endl;
}
// system("pause");
if(fileContent.attrib==_A_SUBDIR&&strcmp(fileContent.name,"..")!=0&&strcmp(fileContent.name,".")!=0)
{
chdir(fileContent.name);
scanFileDepth(fileName,numFile,depth+1);
}
}
}
chdir("..");
}
int main()
{
int numFile=0;
scanFileDepth("*",numFile);
cout<<"文件总数:"<<numFile<<endl;
return 0;
}
这里面做了一下文件结构的格式,你不需要可以将格式输出部分注释了,你若要指定特定的后缀,可以再主程序的scanFileDepth("*",numFile)中把“*”改了,如.txt文件改成".txt".
关于第二个问题我也没看懂你想表达什么....对某些特定的字符串添加标注应该是不难的.
// inserting into a string
#include <iostream>
#include <string>
int main ()
{
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
std::cout << str << '\n';
return 0;
}
哪里需要循环