C++学习

basic_string::at

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

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;
}
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

参考

std::string::find

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

str
Another  string with the subject to search for.
pos
Position 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.
s
Pointer 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.
n
Length of sequence of characters to match.
c
Individual 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).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值