Home > Back-end >  C small white for help!
C small white for help!

Time:11-22

I c + + white, beginner to STL, could you tell me how to do this problem? Can be complete code, very grateful!
Given a string, converted to another string, each rule is composed of two parts: a word may appear in the input file and a phrase used to replace it, express the meaning of is, when the first word in the input, we will he replaced with corresponding phrase,
Word conversion file rules are as follows:
BRB - & gt; Be right back
K - & gt; okey?
Y - & gt; According to
R - & gt; All
U - & gt; You
PIC - & gt; Picture
THK - & gt; thanks!
R - 18 & gt; Later
Hope to convert the content to:
Where r u
Y dont u send me a PIC
THK 18 k r
Note: using the map to complete this topic,

CodePudding user response:

To replace and replace with the map after relationship established, and then read from the file, keyword to replace

CodePudding user response:

 
# include & lt; stdio.h>
# include & lt; String>
# include & lt; Map>
# include & lt; Iostream>
# include & lt; Vector>

The static const char * arr [] [2]={
{" BRB ", "be right back"},
{" k ", "okey?" },
{" y ", "according to"},
{" r ", "motorcycle"},
{" u "and" you "},
{" PIC ", "picture"},
{" THK ", "thanks!" },
{18 "r", "later"},
};

Int the split (const STD: : string& The SRC, const STD: : string & amp; The pattern, STD: : vector & VCT) {
If (SRC) empty ()) {
return 0;
}

Int start=0;
Int independence idx=SRC. Find_first_of (pattern, start);

While (independence idx!=STD: : string: : npos) {
If (start! Independence idx)={
STD: : string TMP=SRC. Substr (start, (independence idx - start));
VCT. Push_back (TMP);
}

Start independence idx=+ 1;
Independence idx=SRC. Find_first_of (pattern, start);
}

if (! The SRC. Substr (start). The empty ()) {
VCT. Push_back (SRC) substr (start));
}

return 0;
}

Int main ()
{
STD: : string strSrc="HTTP://https://bbs.csdn.net/topics/where r u y dont u send me a PIC THK 18 k r";
STD: : map The mapping.
Int size=sizeof (arr)/sizeof (* arr);

for (int i=0; I & lt; The size; I++) {
Mapping. Insert (STD: : make_pair (arr [I] [0], arr [I] [1]));
}

STD: : vector Words;
The split (strSrc, "", words);

STD: : string result="";
for (int i=0; I & lt; Words. The size (); I++) {
STD: : string& TMP=words [I];

STD: : map : : iterator it=mapping. The find (TMP);
If (it!=mapping. The end ()) {
TMP=it - & gt; Second;
}

The result +=TMP;
If (I.=(words. The size () - 1)) {
The result +="";
}
}

STD: : cout & lt;
return 0;
}

CodePudding user response:

"Post, give points
  • Related