Skip to content

Instantly share code, notes, and snippets.

@LusainKim
Last active June 6, 2016 15:02
Show Gist options
  • Save LusainKim/f276155213071f739a0f409c41aa6fe2 to your computer and use it in GitHub Desktop.
Save LusainKim/f276155213071f739a0f409c41aa6fe2 to your computer and use it in GitHub Desktop.
C로 짜는 std::vector<std::vector<int>>
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define CONTAIN_TYPE int
typedef struct {
int iSize;
CONTAIN_TYPE *iArr;
}Contain;
typedef struct {
int iSize;
Contain* cArr;
}vector;
int main()
{
vector v;
v.iSize = 0;
v.cArr = NULL;
printf("to use array, input -1\n");
while (1)
{
int iSize = 0;
// input Arr Size
scanf("%d", &iSize);
rewind(stdin);
if (iSize == -1) goto ShowResult;
// Create Contain
Contain newContain;
newContain.iSize = iSize;
newContain.iArr = malloc(sizeof(CONTAIN_TYPE) * iSize);
for (int i = 0; i < iSize; ++i) newContain.iArr[i] = i * 10;
// Add in vector
v.iSize++;
// New cArr
Contain* Ctemp = malloc(sizeof(Contain) * v.iSize);
for (int i = 0; i < v.iSize - 1; ++i)
Ctemp[i] = v.cArr[i];
Ctemp[v.iSize - 1] = newContain;
// Release old cArr
if(v.cArr != NULL)
free(v.cArr);
// swap
v.cArr = Ctemp;
}
ShowResult:
printf("Result : \n");
for (int i = 0; i < v.iSize; ++i)
{
printf("%d : %d\n", i, v.cArr[i].iSize);
}
int iSelect = 0;
printf("Select Show Array : ");
do {
scanf("%d", &iSelect);
rewind(stdin);
} while (iSelect < 0 || iSelect >= v.iSize);
for (int i = 0; i < v.cArr[iSelect].iSize; ++i)
printf(" %d \n", v.cArr[iSelect].iArr[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment