Memcpy 배열 복사 - Memcpy baeyeol bogsa

#define _CRT_SECURE_NO_WARNINGS

void PRINT(int* num, char* buf, EXAM *exam)

    printf("buf2 = %s\n", buf);

    printf("exam2[0].number = %d\n", exam[0].number);

    printf("exam2[0].name = %s\n", exam[0].name);

    printf("exam2[1].number = %d\n", exam[1].number);

    printf("exam2[1].name = %s\n", exam[1].name);

    int num1[5] = { 10, 20, 30, 40, 50 }; //초기값 설정

    char buf1[50] = "hello maincodes !";

    strcpy(exam1[0].name, "the codes");

    strcpy(exam1[1].name, "maincodes");

    for (i = 0; i < strlen(buf1); i++)

    PRINT(num2, buf2, exam1);

    //memset함수를 이용하여 메모리 배열 메모리 초기화

    memset(num2, 0, sizeof(num2));

    memset(buf2, 0, sizeof(buf2));

    memcpy(num2, num1, sizeof(num1));

    memcpy(buf2, buf1, sizeof(buf1));

    memcpy(&exam2, exam1, sizeof(exam1)); //구조체 복사

    PRINT(num2, buf2, exam2);

    memset(num2, 0, sizeof(num1)); //배열

    memset(buf2, 0, sizeof(buf1)); //배열

    memset(&exam2, 0, sizeof(exam2)); //구조체 초기화

    PRINT(num2, buf2, exam2);

memcpy

#include <string.h>  // C++ 에서는 <cstring>

void* memcpy(void* destination, const void* source, size_t num);

예제

memcpy(arr, tarr, sizeof(arr));

 

copy

#include <algorithm>

copy(initArray, initArray + n, v.begin()); // initArray내용을 n만큼 v에 복사

copy(v.begin(), v.end(), v2.begin()); // v의 내용을 v2에 복사

 

상세

template<class InputIterator, class OutputIterator>
  OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result)
{
  while (first!=last) {
    *result = *first;
    ++result; ++first;
  }
  return result;
}

예제

#include <iostream>
#include <algorithm>    // copy
#include <vector>       // vector
using namespace std;
int n = 7;
int main() {
	vector<int> v(n);
	int initArray[] = { 10,20,30,40,50,60,70 };

	copy(initArray, initArray + n, v.begin());

	cout << "v contains:";
	for (vector<int>::iterator it = v.begin(); it != v.end(); ++it)
		printf("%d ", *it);
	
	// vector<int> v2; 크기가 없어서 복사 실패
	vector<int> v2;
	v2.resize(n);
	copy(v.begin(), v.end(), v2.begin());

	vector<int>::iterator iter = v2.begin();
	printf("\n%d", iter[3]);

	return 0;
}

실행결과

Memcpy 배열 복사 - Memcpy baeyeol bogsa

 

공유하기

게시글 관리

구독하기DR-Kim

저작자표시 비영리 변경금지

  • 카카오스토리
  • 트위터
  • 페이스북

'💎 > C++' 카테고리의 다른 글

[C++] cin, cout 시간 초과 문제 해결  (0)2020.10.16[C++] priority_queue 우선순위 큐  (0)2020.03.25[C++] map iterator(반복자) 활용  (0)2020.03.23[C++] string 메소드 정리  (0)2020.03.17[C++] vector 배열 중복 제거 하는 법  (0)2020.03.10

배열 내용을 다른 배열에 한번에 복사하기

1.1) 실습

설정


이번에는, 배열의 내용을 다른 배열로 한번에 복사하는 예제를 보여드리겠습니다.


#include <stdio.h>
#include <memory.h> // or #include <string.h>

#define ARR_SIZE 10

int main(int argc, char* argv[]){
	int dstArr[ARR_SIZE];
	int srcArr[ARR_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
	int i;
	memcpy(dstArr, srcArr, sizeof(srcArr)); // or memcpy(dstArr, srcArr, sizeof(int) * ARR_SIZE);
	for (i = 0; i < ARR_SIZE; i++){
		printf("%d ", dstArr[i]);
	}
	printf("\n");
	return 0;
}


2 라인

memcpy 함수를 사용하기 위해서는, memory.h 혹은 string.h 를 include 하면 됩니다.


4 라인

배열 요소의 개수를 상수화합니다.


7 라인

복사할 목적지가 되는 배열을 선언합니다.


8 라인

복사할 대상이 되는 배열을 선언하고, 1 부터 10 까지의 정수로 초기화합니다.


10 라인

memcpy 함수를 이용하여, srcArr 의 배열의 시작 주소부터 시작해서, 이 배열의 전체 크기만큼 모두 한번에, 요소 값들을 dstArr 배열로 복사합니다.


11 ~ 14 라인

복사된 결과를 출력합니다.


[실행 결과]

Memcpy 배열 복사 - Memcpy baeyeol bogsa




(memcpy 함수 설명 참고)

https://msdn.microsoft.com/en-us/library/aa246468(v=vs.60).aspx

공유하기

게시글 관리

구독하기kkikkodev 의 IT 이야기

저작자표시

'1.1) 실습' 카테고리의 다른 글

1 초에 한 번씩 데이터 출력하기  (0)2015.06.08프로그램 강제로 종료시키기  (0)2015.06.08콘솔 화면 지우기  (6)2015.06.08소리 및 음악 재생하기 (1) - WinApi  (4)2015.05.25의사 난수 (랜덤 숫자) 생성하기  (0)2015.05.17변수를 특정 값으로 초기화하기 (or 배열을 특정 값으로 한번에 초기화하기)  (0)2015.05.17현재 틱 카운트 (TickCount) 를 이용하여 시간 재기  (0)2015.05.17화면 출력 (에코) 없이 키보드 입력 값 가져오기  (2)2015.05.17비동기적으로 사용자 입력이 있는지 확인하기  (0)2015.05.05콘솔 글자 색 변경하기  (4)2015.04.10

COMMENT :1

by kkikkodev 2015. 5. 17. 22:52

댓글

    name password homepage

    비밀글

    comment 댓글 남기기