cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Strings library : string : string::copy
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
Strings library
char_traits
classes:
· string
global functions:
· getline
· operator+
· operator<<
· operator>>
· comparison operators
· swap
string
string::string
member constants:
· string::npos
member functions:
· string::append
· string::assign
· string::at
· string::begin
· string::capacity
· string::clear
· string::compare
· string::copy
· string::c_str
· string::data
· string::empty
· string::end
· string::erase
· string::find
· string::find_first_not_of
· string::find_first_of
· string::find_last_not_of
· string::find_last_of
· string::get_allocator
· string::insert
· string::length
· string::max_size
· string::operator+=
· string::operator=
· string::operator[]
· string::push_back
· string::rbegin
· string::rend
· string::replace
· string::reserve
· string::resize
· string::rfind
· string::size
· string::substr
· string::swap

-

string::copy public member function
size_t copy ( char* s, size_t n, size_t pos = 0) const;

Copy sequence of characters from string

Copies a sequence of characters from the string content to the array pointed by s. This sequence of characters is made of the characters in the string that start at character position pos and span n characters from there.

The function does not append a null character after the content copied. To retrieve a temporary c-string value from a string object, a specific member function exists: c_str.

Parameters

s
Array where the sequence of characters is copied. The storage space of the array shall already be allocated and should be large enough to contain n characters.
n
Number of characters to be copied. If this number is greater than the amount of characters between pos and the end of the string content, only those characters between pos and the end of the string are copied.
pos
Position of the first character to be copied from the string.
out_of_range is thrown if pos>size().

Return Value

The effective length of the sequence of characters copied to s. This may either be equal to parameter n or to size()-pos, whichever is smaller (see description of parameter n).

size_t is an unsigned integral type.

Example

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

int main ()
{
  int length;
  char buffer[20];
  string str ("Test string...");
  length=str.copy(buffer,6,5);
  buffer[length]='\0';
  cout << "buffer contains: " << buffer << "\n";
  return 0;
}

Output:

Buffer contains: string

Basic template member declaration

( basic_string<charT,traits,Allocator> )
typedef typename Allocator::size_type size_type;
size_type copy( charT* s, size_type n, size_type pos = 0) const;

See also

string::assign Assign content to string (public member function)
string::c_str Get C string equivalent (public member function)
string::replace Replace part of string (public member function)
string::insert Insert into string (public member function)

Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us