Last active
September 10, 2019 21:43
-
-
Save ezirmusitua/56680553cb075bb60d360cd8fe900ffe to your computer and use it in GitHub Desktop.
[Copy string] copy string #C++ #string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// with stl | |
int a[] = { 1, 2, 3, 4, 5 }; | |
int b[5]; | |
std::copy(std::begin(a),std::end(a),std::begin(b)); | |
for(auto e:b) cout << e << " "; // 1 2 3 4 5 | |
// array container (C++11) | |
std::array<int,5> arr = { 1, 2, 3, 4, 5 }; | |
std::array<int,5> copy; | |
copy = arr; | |
arr[0] = 100; | |
for(auto e:copy) cout << e << " "; // 1 2 3 4 5 | |
// C style - memcpy | |
int arr[] = {1,2,3,4,5}; | |
int copy[5]; | |
int len = sizeof(arr) / sizeof(arr[0]); | |
// void* memcpy(void* destination,const void* source,size_t num); | |
memcpy(copy, arr, len * sizeof(int)); | |
for(auto e:copy) cout<< e << " "; // 1 2 3 4 5 | |
// C style - memmove | |
int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; | |
memmove(arr + 3, arr + 1, sizeof(int) * 5); | |
for(auto e:arr) cout<< e << " "; // 1 2 3 2 3 4 5 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment