善用STL,节约代码量

在csdn上大家寫出了各種方式-@-可是... 都太復雜了 如
1 :

void replace(char *pInput, char *pOutput, char *pSrc, char *pDst)
{
char *pi, *po, *p;
int nSrcLen, nDstLen, nLen;

pi = pInput;
po = pOutput;
nSrcLen = strlen(pSrc);
nDstLen = strlen(pDst);

p = strstr(pi, pSrc);
if(p)
{
while(p)
{
nLen = (int)(p - pi);
memcpy(po, pi, nLen);
memcpy(po + nLen, pDst, nDstLen);
pi = p + nSrcLen;
po = po + nLen + nDstLen;
p = strstr(pi, pSrc);
}
strcpy(po, pi);
}
else
{
strcpy(po, pi);
}
}

2

Fun(char *A, char *B, char *C)
{
int lenA = strlen(A);
int lenB = strlen(B);
int lenC = strlen(C);
int Count = 0;
int j = 0;
int k = 0;
while ((j < lenA) && (i < lenB))
{
if ( A[j] == B[i] || (A[j] - '32' == B[i]) || (A[j] + '32' == B[i]))
{
j++;
i++;
Count++;
}
else
{
Count = 0;
i = 0;
j = j - i +1;
}
}
if (j >= lenB)
{
pos = i - lenB;
}
for (;pos < lenB&& Count--; pos++)
{
A[pos] = C[k++];
}

}

3

void find_rep(char *a, char *b, char *c)
{
int lena, lenb, lenc;
lena = strlen(a);
lenb = strlen(b);
lenc = strlen(c);

std::cout << "a:/t" << a <<endl
<< "b:/t" << b << endl
<< "c:/t" << c <<endl;
std::cout << "len:/t" << lena << "/t" << lenb << "/t" << lenc << endl;

int i, j, k, sanum;
int istore=0;

for(i=0; i<lena;)
{
sanum=0;
for(j=0; j<lenb&&i<lena;)
{
if(a[i]==b[j]||a[i]+32==b[j]||a[i]-32==b[j])
{
i++;
j++;
sanum++;
}

else
{
i=i-j+1;
j=0;
sanum=0;
}
}
if(sanum==lenb)
{
istore=i-sanum; //orin i
j=0;

for(istore, j; j<lenc; j++,istore++)
a[istore] = c[j];
j=i;//now i
i=i+lenc-lenb; //next i
for(k=i; k<lena+lenc-lenb; k, j++;)
a[k] = a[j];
a[k] = '/0';
lena = strlen(a);
}
}
std::cout << a << endl;
//system("pause");
}

4 最可愛的就是一個哥們了 他寫的代碼好奇怪 號稱用了STL可是....

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <iterator>

using namespace std;

class Comp
{
public:
bool operator()( const char& Elem1, const char& Elem2 )
{
return ToUpper(Elem1) == ToUpper(Elem2);
}

private:
char ToUpper( const char& ch )
{
return ch>='a' && ch<='z' ? ch-'a' + 'A' : ch;
}
};

string SearchAndReplace(const string& src, const string& searchment, const string& replacement )
{
vector<string::const_iterator> idxs;

string::const_iterator it = search( src.begin(), src.end(),
searchment.begin(), searchment.end(), Comp() ) ;

for( ;
it!= src.end();
it=search( it, src.end(),
searchment.begin(), searchment.end(), Comp() ) )
{
idxs.push_back( it );
it += searchment.size();

}

string result;
string::const_iteratorit_s = src.begin();
for ( vector<string::const_iterator>::iterator it = idxs.begin(); it != idxs.end(); ++it )
{
string::const_iterator it_e = *it;

copy( it_s, it_e, back_inserter( result ) );
copy( replacement.begin(), replacement.end(), back_inserter( result ) );

it_s = it_e + searchment.size();
}

if ( it_s != src.end() )
copy ( it_s, src.end(), back_inserter( result ) );

return result;
}

int main()
{
cout<< fun( "aaAbb", "ab", "kkk" ) ;

getchar();
}


當當當當
正確答案在這里

string function(string a, string b, string c)
{
    int n = b.size();
    int pos = 0;
    while( (pos = a.find(b,pos)) != -1)
    {
         a.replace( pos, n, c );
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值