字符串搜索(一种更好的方法) [英] String Search (A Better Way)

查看:74
本文介绍了字符串搜索(一种更好的方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个包含36个实例的字符串搜索,出于理智和空间考虑,这里仅显示4个实例.有一个更好的方法吗.也许是地图?

I''m doing a string search that has 36 instances, I only show 4 here for sanity sake and space. Is there a better way to do this. Maybe a map?

#include <string>
#include <iostream>

int Count( const std::string & str, 
           const std::string & obj ) {
    int n = 0;
	int p = 0;
	int q = 0;
	int r = 0;
    std::string ::size_type pos = 0;
    while( (pos = obj.find( str, pos )) 
                 != std::string::npos ) {
    	n++;
		p++;
		q++;
		r++;
    	pos += str.size();
    }
	return n;
	return p;
	return q;
	return r;
}

int main() {
    std::string s = "How do you do at ou";
    int n = Count( "o", s );
	int p = Count( "d", s );
	int q = Count( "u", s );
	int r = Count( "t", s );
    std::cout << n << std::endl;
	std::cout << p << std::endl;
	std::cout << q << std::endl;
	std::cout << r << std::endl;
	system ("pause");
}

推荐答案

Frnd!

试试这个!

Frnd!

Try this!

# include <iostream>
#include "afxwin.h"
using  namespace std;
int GetCount(char a,CString s);
int main()
{
	CString s = "aaabbcdfaeaaaeeeeeeeeeeeeeeeeeeeea";
	int nCount_a = GetCount('a',s);
	cout << nCount_a << endl;
	int nCount_e = GetCount('e',s);
	cout << nCount_e << endl;
	
}

int GetCount(char ch, CString str)
{
	int nCount = 0;
	int  n = 0;
	for(int i = 0; i < str.GetLength(); i++)
	{
		 
		if ( str.Find(ch,i) == i)
		{
			nCount ++;
		}
	}
	return nCount;
}</iostream>


老实说,如果您所做的只是计算每个字符的实例,我只对可恶的东西进行排序并计算重复次数. ..
To be honest, if all you are doing is counting the instances of each character, I would just sort the damn thing and count the repetitions...


我已经厌倦了这篇文章"Bjarne Stroustrup的经典书籍"C ++编程语言"中的标准库" 可从其站点上免费获得.当然,您可以阅读22页来获得更好的主意.
I am already tired to mention this article "A Tour of the Standard Library" from classic book "The C++ Programming Language" by Bjarne Stroustrup available for free from his site. Sure, you may get some better ideas reading its 22 pages.


这篇关于字符串搜索(一种更好的方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆