2
구조체의 배열 개념에 많은 어려움을 겪고 있습니다. 기본 코드를 정리했습니다. 이 코드의 결과는 내가 예상 한 바가 아니 었습니다. 누군가이 코드가 왜 그런 식으로 행동하는지 설명 할 수 있는지 궁금합니다.이 코드의 동작을 설명하는 데 도움이 필요합니다.
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
struct DataRow
{
std::string word1;
std::string word2;
std::string word3;
};
void getRow(std::string line, DataRow** dataRowPointer, int index)
{
std::stringstream sline(line);
std::string word;
int wordIndex = 0;
*dataRowPointer = new DataRow[3];
while(sline >> word)
{
if (wordIndex == 0)
{ (*dataRowPointer)[index].word1 = word; }
else if (wordIndex == 1)
{ (*dataRowPointer)[index].word2 = word; }
else
{ (*dataRowPointer)[index].word3 = word; }
wordIndex++;
}
}
int main()
{
int index = 0;
std::string line1 = "This is line";
std::string line2 = "Two is magic";
std::string line3 = "Oh Boy, Hello";
DataRow* dataRowPointer;
getRow(line1, &dataRowPointer, index);
index++;
getRow(line2, &dataRowPointer, index);
index++;
getRow(line3, &dataRowPointer, index);
for(int i = 0; i < 3; i++)
{
std::cout << dataRowPointer[i].word1 << dataRowPointer[i].word2 << dataRowPointer[i].word3 << "\n";
}
return 0;
}
3 개의 문자열이 있습니다. 각 문자열 내의 각 단어를 분리하여 구조에 저장하려고합니다. 나는 그들을 저장할 수있는 배열을 가지고있다. 배열의 크기는 3입니다 (3 줄이 있기 때문에). 나는 포인터를 메인에 설정하지 않고 포인터를 내 함수에 설정한다. 거기에서 나는 가게 할 나의 말을 모으기 시작한다.
이(blank line)
(blank line)
OhBoy,Hello
내 질문은, 처음 두 구조 갔었어요된다
나는이 출력을 얻을?