关于CString类中的Find函数!

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于CString类中的Find函数!相关的知识,希望对你有一定的参考价值。

test="abcdefg"
cout<<csStr.Find('d');
cout<<csStr.Find("d",1);
cout<<csStr.Find('d',2);
cout<<csStr.Find('d',3);
cout<<csStr.Find('d',4);
cout<<csStr.Find('d',5);

分别输出什么,乱乱的,要理一下!

参考技术A 呵呵:MSDN:的秒速如图The zero-based index of the first character in this CString object that matches the requested substring or characters; -1 if the substring or character is not found.
也就是基于下标为0的的查找;第一个是查找的字符,第二个是查找的开始位置;返回的是字符串的位置,如果为找到返回-1;大哥。。。。。。。。。。
于是csStr.Find('d'); //其实位置为0,可以找到明显为3;
。Find(‘d’,1)//明显是b开头可以找到。是3
下面的是csStr.Find('d',2); //可以还找到3
csStr.Find('d',3);//刚好从‘d’出来时找,当然可以为3
下面的都是-1;因为找不到。。因此返回-1追问

现在明白多了那如果是
csStr="abcaefg"
cout<<csStr.Find('a");
cout<<csStr.Find('a',2);
它们返回的是什么呢

追答

0,3,第一个是0,明显第一个是a;
第二个是3,因为从c初开始找,明显是地4个字符a

追问

谢谢!那样我明白了!

本回答被提问者采纳
参考技术B 通常来说,find函数用于寻找某个序列的在string中第一次出现的位置。

find函数有以下四种重载版本:
size_t find (const string& str, size_t pos = 0) const noexcept;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_type n) const;
size_t find (char c, size_t pos = 0) const noexcept;
参数说明:
str/s/c:要寻找的序列,可以是字符串(版本1),也可以是字符串字面值或者说C风格字符串(版本2、3,在版本3中,所寻找的序列是从s[0]开始的前n个字符),也可以是字符(版本4)。
pos:从string的pos位置开始寻找(注意第一个位置是0)。
函数返回序列第一次出现的位置,如果没有找到则返回string::npos。

样例:(摘自cplusplus.com)
#include <iostream> // std::cout
#include <string> // std::string

int main ()

std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");

// different member versions of find in the same order as above:
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';

found=str.find("needles are small",found+1,6);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n';

found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n';

found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n';

// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '\n';

return 0;

标准输出结果:
first 'needle' found at: 14
second 'needle' found at: 44
'haystack' also found at: 30
Period found at: 51
There are two prepositions in this haystack with needles.
参考技术C 4,3,2,1,0

使用基类中的函数静态调用纯虚函数的派生类实现

【中文标题】使用基类中的函数静态调用纯虚函数的派生类实现【英文标题】:Calling a derived-class implementation of a pure virtual function statically using a function in the base class 【发布时间】:2014-05-02 15:11:29 【问题描述】:

在***的其他地方有一些关于这个话题的讨论,但我还没有真正找到我的问题的明确答案。

我的设置是这样的:

class BaseClass

    virtual short return_number_for_thing1(std::string thing1)=0; //note the pure virtual
    virtual short return_number_for_thing2(std::string thing2)=0;
    virtual short return_number_for_thing(std::string thing); //virtual is probably not necessary here, I can get rid of it if need be


short BaseClass::return_number_for_thing(std::string thing)
 //pretend thing1 and thing2 are enum'ed somewhere
    if      (thing == thing1) return return_number_for_thing1(thing);
    else if (thing == thing2) return return_number_for_thing2(thing);


class DerivedClass1 : BaseClass

    short return_number_for_thing1(std::string thing1);
    short return_number_for_thing2(std::string thing2);


class DerivedClass2 : BaseClass

    short return_number_for_thing1(std::string thing1);
    short return_number_for_thing2(std::string thing2);

我的问题是,为什么我不能写这样的代码:

short number_i_want = DerivedClass2::return_number_for_thing(thing);

我有点理解尝试从 BaseClass 指针调用 return_number_for_thing 没有意义,因为它不知道是调用 DerivedClass1 还是 DerivedClass2 的例程,但如果我给它 DerivedClass2 的范围,应该它能够弄清楚我想要什么吗?现在,我在需要时创建了一个 DerivedClass2 或 DerivedClass1 的空白实例,但在我看来我不应该这样做。

【问题讨论】:

没有一个方法被声明为static,所以它们需要一个类的实例才能被调用。 没有像静态虚函数这样的东西,句号。 嗯,这是麻烦的一部分。我可以将 return_number_for_thing 声明为静态,但我不能将 return_number_for_thing1 或 return_number_for_thing2 声明为静态,即使在派生类中也是如此,因为编译器告诉我它不能既是虚拟的又是静态的。将 return_number_for_thing 声明为静态是不够的。 拥有多态静态方法简直是个悖论,如果你已经知道类型就不需要多态了吗? πάντα ῥεῖ,如果你喜欢,建议一个不同的标题 【参考方案1】:

在 C++ 中,虚拟和静态不能混用。

virtual = 具体操作取决于对象的类型。 static = 你不需要对象。

当然可以想象这样的事情。如果 C++ 有类似元类型的东西,允许您将常规类型视为对象,那么它就不再是一个奇怪的想法了。

伪代码(使用虚构的语法):

void f(Class base_class)

   base_class.StaticMethod();


struct Base

  virtual static StaticMethod(); // impossible in C++
;

struct Derived : Base

  virtual static StaticMethod(); // impossible in C++
;

f(Base); // impossible in C++
f(Derived); // impossible in C++

创建静态虚函数之类的东西的愿望有时是真正需要的症状(C++ 无法立即满足):将类型视为对象。

【讨论】:

【参考方案2】:

您可以创建一个函数 virtual 或 static ,而不是两者兼而有之。

虚函数必须有一个需要对象实例的 vtable 指针,而静态函数(根据定义)不能有与其相关的实例。

如果您将函数声明为例如:

virtual short return_number_for_thing1(std::string thing1)=0;

那么您必须创建一个实例并使用它,例如m_Instance-&gt;return_number_for_thing1(...)(但由于您没有使用任何成员变量,这似乎没有任何意义)。

如果您将函数声明为静态:

static short return_number_for_thing1(std::string thing1)

然后您必须静态调用函数BaseClass::return_number_for_thing1(),如果您在派生类中提供相同函数的静态版本,它现在将成为覆盖,您可以选择在编译时使用BaseClass::return_number_for_thing1 或@ 调用哪个函数987654326@..

您的问题仍然没有真正的意义,因为您不能静态使用虚函数。静态暗示你想在没有任何实例的情况下使用它,它就像一个 c 函数但隐藏在类的命名空间中(并且遵守隐私)。虚函数需要一个实例来操作。

【讨论】:

【参考方案3】:

制作static virtual 方法是不可能的,引用C++11 standard N3337 - 10.3.10:

[ 注意:virtual 说明符意味着成员资格,所以 virtual 函数不能是非成员 (7.1.2) 函数。 虚拟也不能 函数是静态成员,因为虚函数调用依赖于 用于确定调用哪个函数的特定对象。一个虚拟的 在一个类中声明的函数可以在另一个类中声明为友元 班级。 ——尾注]

【讨论】:

显然我的标题已经将注意力从实际问题上移开......我已经更改标题以更能反映我实际尝试做的事情 虚拟函数与实例绑定。您可以创建静态实例(单例)并像这样使用它:DerivedClass2::Instance().return_number_for_thing(thing);

以上是关于关于CString类中的Find函数!的主要内容,如果未能解决你的问题,请参考以下文章

VC++中CString里的find函数怎么使用

CString之Find()FindOneOf()ReverseFind()

CString之Find()FindOneOf()ReverseFind()

关于js查找和筛选的几种方式

关于VBA的Find函数

string类find函数返回值判定