find_first_of (STL Sample)
来源:互联网 发布:c 有网络编程吗 编辑:程序博客网 时间:2024/06/17 04:32
basic_string::find_first_of (STL Sample)
The sample code below illustrates how to use the basic_string::find_first_of STL function in Visual C++.
Required Header:<string>
Prototype:
size_type find_first_of(const basic_string& _X, size_type iPos = 0);
size_type find_first_of(const element_type *_S, size_type iPos, size_type cElementsIn_S);
size_type find_first_of(const element_type *_S, size_type iPos = 0);
size_type find_first_of(element_type _C, size_type iPos = 0);
Note: The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.
Description:The member functions each find the first (lowest position) element of the controlled sequence at or after position iPos that matches any of the elements in the operand sequence specified by the remaining operands. If it succeeds, it returns the position. Otherwise, the function returns npos. The position returned is 0 (zero) based. The npos return value is a special value indicating that none of the elements were found.
Sample Code:
//////////////////////////////////////////////////////////////////////
//
// Compile options needed: /GX
//
// main.cpp : Demonstrates the use of the string::find_first_of().
//
// Functions:
//
// string::find_first_of() - find the first instance in the
// controlled string of any of the elements specified by the
// parameters. The search begins at an optionally-supplied
// position in the controlled string.
//////////////////////////////////////////////////////////////////////
#include <string>
#include <iostream>
using namespace std ;
void main()
{
string str1("Heartbeat");
string str2("abcde");
int iPos = 0;
cout << "The string to search is '" << str1.c_str() << "'"
<< endl;
// find the first instance in str1 of any characters in str2
iPos = str1.find_first_of (str2, 0); // 0 is default position
cout << "Element in '" << str2.c_str() << "' found at position "
<< iPos << endl;
// start looking in the third position...
iPos = str1.find_first_of (str2, 2);
cout << "Element in '" << str2.c_str() << "' found at position "
<< iPos << endl;
// use an array of the element type as the set of elements to
// search for; look for anything after the fourth position
char achVowels[] = {'a', 'e', 'i', 'o', 'u'};
iPos = str1.find_first_of (achVowels, 4, sizeof(achVowels));
cout << "Element in '";
for (int i = 0; i < sizeof (achVowels); i++)
cout << achVowels[i];
cout << "' found at position " << iPos << endl;
// use a string literal to specify the set of elements
char szVowels[] = "aeiou";
iPos = str1.find_first_of (szVowels, 0); // 0 is default position
cout << "Element in '" << szVowels << "' found at position "
<< iPos << endl;
// look for a specific character beginning in the third position
iPos = str1.find_first_of ('e', 2);
cout << "'e' found at position " << iPos << endl;
}
Program Output is:
The string to search is 'Heartbeat'
Element in 'abcde' found at position 1
Element in 'abcde' found at position 2
Element in 'aeiou' found at position 6
Element in 'aeiou' found at position 1
'e' found at position 6
- find_first_of (STL Sample)
- STL(5)find_first_of函数
- STL algorithm算法find_first_of(18)
- C++的STL之find_first_of
- find_first_of
- find_first_of
- find_first_of
- find_first_of
- find_first_of
- find_first_of
- STL可能的误用-find_first_of和erase
- STL 之find,find_if,find_end,find_first_of
- STL 之find,find_if,find_end,find_first_of
- append (STL Sample)
- stl string的substr和find_first_of函数的一点心得
- C++ STL 算法:查找算法(4)find_first_of
- size and resize (STL Sample)
- c++之STL(13) STL 算法 - 查找算法(4)find_first_of(b,e,sb,se) find_first_of(b,e,sb,se,bp)
- SQL Server Logins and Users - Orphaned Users
- Qt 弹出对话框方法
- 对状态模式的理解及其应用(佣金报表,单用户佣金查询)
- 转:js脚本中的eval在火狐下不能运行
- keep code tight&clean
- find_first_of (STL Sample)
- 正则表达式集
- VC与Python交互(一)(静态编译Python的方法)
- 显示隐藏控件的属性
- SP2010: SharePoint Microsoft Certified Master Program
- size and resize (STL Sample)
- amoeba------介于DB和客户端之间的代理
- Android UI 设计秘笈 :Part I
- ubuntu下安装mysql