求c++ find的用法总结,最好有实例。

干饭人之路 2007-12-10 08:14:26
我想在一个wstring串中查找指定的字符串。
...全文
4989 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
tanlijun37 2007-12-13
  • 打赏
  • 举报
回复
L 代表宽文本
干饭人之路 2007-12-12
  • 打赏
  • 举报
回复
2楼tangshuiling :
wstring str(L"abcdefghijklmnopqrst");//假设要找fgh
str.find(L"gh"); //函数返回6表示字串在6号位置

为什么总是有个大写的"L"???
ryfdizuo 2007-12-10
  • 打赏
  • 举报
回复

size_t find ( const string& str, size_t pos = 0 ) const;
size_t find ( const char* s, size_t pos, size_t n ) const;
size_t find ( const char* s, size_t pos = 0 ) const;
size_t find ( char c, size_t pos = 0 ) const;

Find content in string

Searches the string for the content specified in either str, s or c, and returns the position of the first occurrence in the string.

When pos is specified the search only includes characters on or after position pos, ignoring any possible occurrences in previous locations.

Notice that unlike member find_first_of, whenever more than one character is being searched for, it is not enough that only one of these characters match, but the entire sequence of characters to find must be matched.


Parameters
str
string to be searched for in the object. The entire content of str must be matched in some part of the string to be considered a match.
s
Array with a sequence of characters.
In the second member function version, the size of the content to be matched is only determined by parameter n.
In the third version, a null-terminated sequence is expected, and its end is determined by the first occurrence of a null character in it.
n
Length of sequence of characters to search for.
c
Individual character to be searched for.
pos
Position of the first character in the string to be taken into consideration for possible matches. A value of 0 means that the entire string is considered.

Return Value
The position of the first occurrence in the string of the searched content.
If the content is not found, the member value npos is returned.

Example
// string::find
#include <iostream>
#include <string>
using namespace std;

int main ()
{
string str ("There are two needles in this haystack with needles.");
string str2 ("needle");
size_t found;

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

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

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

found=str.find('.');
if (found!=string::npos)
cout << "Period found at: " << int(found) << endl;

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

return 0;
}


Notice how parameter pos can be used to search for a second instance of the same search string. Output:
first 'needle' found at: 14second 'needle' found at: 44'haystack' also found at: 30Period found at: 51There are two prepositions in this haystack with needles.



Basic template member declarations
( basic_string<charT,traits,Allocator> )
typedef typename Allocator::size_type size_type;
size_type find ( const basic_string& str, size_type pos = 0 ) const;
size_type find ( const charT* s, size_type pos, size_type n ) const;
size_type find ( const charT* s, size_type pos = 0 ) const;
size_type find ( charT c, size_type pos = 0 ) const;

wstring 与string 用法差不多的把
tangshuiling 2007-12-10
  • 打赏
  • 举报
回复
wstring str(L"abcdefghijklmnopqrst");//假设要找fgh
str.find(L"gh"); //函数返回6表示字串在6号位置

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧