A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/string/string/push_back/ below:

public member function

<string>

std::string::push_back

Append character to string

Appends character c to the end of the string, increasing its length by one.

Parameters
c
Character added to the string.

Return value none

Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// string::push_back
#include <iostream>
#include <fstream>
#include <string>

int main ()
{
  std::string str;
  std::ifstream file ("test.txt",std::ios::in);
  if (file) {
    while (!file.eof()) str.push_back(file.get());
  }
  std::cout << str << '\n';
  return 0;
}

This example reads an entire file character by character, appending each character to a string object using push_back.

Complexity Unspecified; Generally amortized constant, but up to linear in the new string length.

Iterator validity Any iterators, pointers and references related to this object may be invalidated.

Data races The object is modified.

Exception safetyStrong guarantee: if an exception is thrown, there are no changes in the string.

If the resulting string length would exceed the max_size, a length_error exception is thrown.


A bad_alloc exception is thrown if the function needs to allocate storage and fails.

See also
string::back
Access last character (public member function)
string::pop_back
Delete last character (public member function)
string::append
Append to string (public member function)
string::insert
Insert into string (public member function)
string::end
Return iterator to end (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4