In C++, the string::copy method is an intrinsic member function of the string class within the Standard Template Library (STL). This function is frequently employed to duplicate a specified quantity of characters from a string to a character array (C-style string). It proves to be quite handy when transitioning between C++ strings (std::string) and C-style character arrays. Essentially, this method duplicates the contents of one string into another string.
Syntax:
It has the following syntax:
str1.copy(str2, len);
str1.copy(str2, len, pos);
In this syntax,
- str2: It represents the destination string object, which retains the copied string.
- len: It defines the length of the substring.
- pos: It determines the position of the first character to include.
Return value
It returns the number of characters to be copied.
Simple C++ String Copy function Example
Let's explore a basic illustration to showcase the string copy method in C++.
Example
#include<iostream>
using namespace std; //using standard namespace
int main() //main function
{
string source = "C++ Tutorial";
char destination[20];
cout<<"The Source string is: "<<source<<'\n';
source.copy(destination,sizeof source);
cout<<"The Destination string is: "<<destination;
return 0;
}
Output:
The Source string is: C++ Tutorial
The Destination string is: C++ Tutorialpool�g��
Explanation:
In this instance, a string titled source has been initialized with the content "C++ Tutorial". Following this, the copy method has been employed to duplicate characters from the string to the character array named destination. The process demonstrates the conversion of data from a C++ string instance to a C-style character array, and subsequently displays both strings.
C++ String Class Example using the copy function with Offset
Let's explore an illustration to showcase the string copy method with an offset in C++.
Example
#include<iostream>
using namespace std; //using standard namespace
int main() //main function
{
string str = "C++ programming language";
char str1[13] ;
str.copy(str1, 8, 5);
str1[8] ='\0';
cout<<"String contains elements: " <<str1;
return 0;
}
Output:
String contains elements: rogrammi
Explanation:
In this instance, we've generated a string labeled str holding the phrase "C++ programming language". Subsequently, we've employed the copy method to duplicate 8 characters commencing from position 5 within str to the char array str1. Following this, we've explicitly added a null terminator ('\0') to signify the conclusion of the duplicated string. Consequently, the result displays the substring "rogrammi".
C++ String Class Example using the copy function to copy elements from an array to a vector
Let's explore an illustration to showcase how to duplicate an array of integers into a vector utilizing the copy method in C++.
Example
#include<iostream>
#include<vector>
using namespace std; //using standard namespace
int main() //main function
{
int a[5]={10, 15, 20, 25, 30};
vector<int> vec(5);
copy(a, a+5, vec.begin());
for(int i=0;i<vec.size();i++)
{
cout<<vec[i];
}
return 0;
}
Output:
1015202530
Explanation:
In this instance, an integer array 'a' with five elements has been established. Subsequently, the std::copy method is employed to duplicate all elements from array 'a' to the vector vec utilizing iterators. To demonstrate the successful transfer of array contents to the vector, a loop is utilized to display the copied vector elements.
C++ String Example using the copy and substr function
Let's consider an instance to showcase manipulating strings with the copy and substr functions in C++.
Example
#include <iostream>
#include <string>
using namespace std; //using standard namespace
int main() { //main function
string str = "C++ Programming Language";
char buffer[20];
// Using the copy() function
size_t copied = str.copy(buffer, 3, 0);
buffer[copied] = '\0';
cout << "Using copy(): " << buffer << endl;
// Using the substr() function
string sub = str.substr(0, 3);
cout << "Using substr(): " << sub << endl;
return 0;
}
Output:
Using copy(): C++
Using substr(): C++
Explanation:
In this illustration, a string named str is initialized with the content "C++ Programming Language". Subsequently, the copy method is employed to duplicate the initial three characters ("C++") from the string to the character array buffer. Following this, a null terminator is manually included to signify the conclusion. The identical outcome is then attained by utilizing the substr method, which generates a substring directly. Lastly, the output from both methods is displayed on the console.
Features of the String copy function in C++
There are several features of the string copy function in C++. Some of them are as follows:
- The string::copy function in C++ is commonly utilized to copy a specified number of characters from a string into a character array (C-style string).
- It helps to ensure that no modification can happen to the original string, and the destination buffer should have enough space to store the string.
- The string copy function allows us to copy a specific part of the string, without requiring the complete string.
- It is very useful when we want to work with APIs or libraries that need C-style strings.
- It is mainly utilized to directly write into an existing buffer without creating a new string object.
- The copy function returns the character counts that are copied into the destination array in C++.
Conclusion
In summary, the string::copy function within C++ offers a straightforward and efficient approach to copying a segment of a string into a character array. This functionality proves to be extremely beneficial for facilitating communication between C++ strings and C-style strings. Moreover, it enhances the ability to manage substrings with greater flexibility while guaranteeing secure data manipulation. Typically, it is employed in conjunction with a null terminator and an adequately sized buffer for optimal performance.
C++ String copy function FAQs
1) What is the String copy function in C++?
In C++, the copy function in the programming language is frequently used to duplicate a specified count of characters from a string to a character array (C-style string). This function is valuable when there is a requirement to connect C++ strings (std::string) with C-style character arrays.
No, the copy function in C++ does not automatically include a null character.
No, C++ does not automatically append a null character. If we need a null-terminated C-string, we must explicitly include \0.
The return type of the string copy function in C++ is a reference to a string.
In C++, the copy function in strings returns a size_t value indicating the count of characters that have been successfully copied.
4) Does the copy method offer better performance compared to the substr function in C++?
Yes, the copy method proves to be more efficient compared to the substr function as it directly writes data into an existing buffer without the need to create a new string object in C++.
5) Is it possible for the copy function to operate with wide strings (wstring) in C++?
Yes, the copy method is compatible with wide strings (wstring) as the wstring class includes an equivalent copy function tailored for handling wide characters in C++.