您的位置:首页 > 编程语言 > C语言/C++

C++学习

2013-12-19 14:22 176 查看


basic_string::at

http://technet.microsoft.com/zh-cn/subscriptions/kfw82kat.aspx

提供对字符在字符串中的指定索引。



const_reference at(
size_type _Off
) const;
reference at(
size_type _Off
);




参数


_Off

元素的位置的索引中引用的。



返回值


为字符串的字符的引用将参数索引指定的位置。



备注


该字符串的第一个元素的索引零,并且正整数顺序标记的以下元素,因此,长度 n 字符串具有该数字标记中的 第n个元素 n
– 1。

该成员 operator[] 比读取提供的成员函数 at 快且为字符串的组件编写。

该成员 operator[] 不检查作为参数传递的索引是否有效,但成员函数 at 执行,所以应使用,则正确性不确定。 无效的索引,该索引零或大于或等于该字符串的大小,通过指向成员函数 at 引发 out_of_range选件类 异常。 无效的索引传递给 operator[] 导致未定义的行为,但是,索引等于该字符串的长度是常数字符串的有效索引,而运算符返回Null字符,同时通过该索引。

返回的引用可能由字符串重新分配或修改无效的非const 字符串的。



示例




// basic_string_at.cpp
// compile with: /EHsc
#include <string>
#include <iostream>

int main( )
{
using namespace std;
string str1 ( "Hello world" ), str2 ( "Goodbye world" );
const string  cstr1 ( "Hello there" ), cstr2 ( "Goodbye now" );
cout << "The original string str1 is: " << str1 << endl;
cout << "The original string str2 is: " << str2 << endl;

// Element access to the non const strings
basic_string <char>::reference refStr1 = str1 [6];
basic_string <char>::reference refStr2 = str2.at ( 3 );

cout << "The character with an index of 6 in string str1 is: "
<< refStr1 << "." << endl;
cout << "The character with an index of 3 in string str2 is: "
<< refStr2 << "." << endl;

// Element access to the const strings
basic_string <char>::const_reference crefStr1 = cstr1 [ cstr1.length ( ) ];
basic_string <char>::const_reference crefStr2 = cstr2.at ( 8 );

if ( crefStr1 == '\0' )
cout << "The null character is returned as a valid reference."
<< endl;
else
cout << "The null character is not returned." << endl;
cout << "The character with index 8 in the const string cstr2 is: "
<< crefStr2 << "." << endl;
}




Output




The original string str1 is: Hello world
The original string str2 is: Goodbye world
The character with an index of 6 in string str1 is: w.
The character with an index of 3 in string str2 is: d.
The null character is returned as a valid reference.
The character with index 8 in the const string cstr2 is: n.




要求


标头: <string>

命名空间: std



请参见



参考

basic_string


http://www.cplusplus.com/reference/string/string/find/


std::string::find

C++98

C++11



string (1)
size_t find (const string& str, size_t pos = 0) const noexcept;

c-string (2)
size_t find (const char* s, size_t pos = 0) const;

buffer (3)
size_t find (const char* s, size_t pos, size_type n) const;

character (4)
size_t find (char c, size_t pos = 0) const noexcept;

Find content in string
Searches the string for the first occurrence of the sequence specified by its arguments.

When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.

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


Parameters

strAnother string with the subject to search for.
posPosition of the first character in the string to be considered in the search.

If this is greater than the string length, the function never finds matches.

Note: The first character is denoted by a value of 0 (not 1): A value of 0 means that the entire string is searched.

sPointer to an array of characters.

If argument n is specified (3), the sequence to match are the first n characters in the array.

Otherwise (2), a null-terminated sequence is expected: the length of the sequence to match is determined by the first occurrence of a null character.

nLength of sequence of characters to match.
cIndividual character to be searched for.

size_t is an unsigned integral type.


Return Value

The position of the first character of the first match.

If no matches were found, the function returns string::npos.

size_t is an unsigned integral type.


Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

// string::find
#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;
}

Notice how parameter pos is used to search for a second instance of the same search string. Output:

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.


Complexity

Unspecified, but generally up to linear in length()-pos times the length of the sequence to match (worst case).


Iterator validity

No changes.


Data races

The object is accessed.


Exception safety

If s does not point to an array long enough, it causes undefined behavior.

Otherwise, the function never throws exceptions (no-throw guarantee).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: