std::array 를 접근하는데있어 에러가 나는 이유를 모르겠습니다.
조회수 494회
#include <vector>
#include <iostream>
#include <random>
#include <chrono>
#include <array>
#include <iostream>
#include <functional>
struct Keys
{
double a;
double b;
Keys(const Keys& k) : a(k.a), b(k.b) {}
Keys() {}
};
using Sequence = std::vector<Keys>;
using RemainingSequence = std::array<Sequence, 4>;
struct ExtremalPoints
{
Keys xMin, xMax, yMin, yMax;
};
using ExistingZones = std::array<bool, 4>;
using RelevantZones = std::array<bool, 4>;
using Location = std::array<signed short int, 4>;
using PointSequenceReducedLastTime = std::array<bool, 4>;
struct ExtremalPointsInZones
{
Keys xMinSW, xMinNW, xMaxSE, xMaxNE, yMinSW, yMinSE, yMaxNW, yMaxNE;
};
struct KeysInfo
{
ExtremalPoints extremalPoints;
RelevantZones relevantZones;
ExtremalPointsInZones extremalPointsInZones;
Location location;
PointSequenceReducedLastTime pointSequenceReducedLastTime {true, true, true, true};
RemainingSequence remainingSequence; // 벡터로 이루어진 4행 어레이 입니다.
};
void generateRandomSequence(Sequence& Seq, size_t numberOfSeq)
{
Seq.clear();
auto seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
auto realRand = std::bind(std::uniform_real_distribution<double>(1,(numberOfSeq)), std::mt19937(seed));
for(size_t i = 0 ; i < numberOfSeq ; i++ )
{
Keys key;
key.a = realRand();
key.b = realRand();
Seq.push_back(key);
}
}
int main()
{
Sequence seq;
size_t numberOfSeq = 10000;
generateRandomSequence(seq, numberOfSeq);
KeysInfo info;
std::cout << "size of array " << info.remainingSequence.size() << "\n";
std::cout << "size of first vector" << info.remainingSequence[0].size() << "\n";
std::cout << "size of second vector" << info.remainingSequence[1].size() << "\n";
std::cout << "size of third vector" << info.remainingSequence[2].size() << "\n";
std::cout << "size of fourth vector" << info.remainingSequence[3].size() << "\n";
std::cout << "size of fifth vector" << info.remainingSequence[5].size() << "\n";
// cout 의 결과는 다음과 같습니다.
// size of array 4
// size of first vector 8795939219482 // 첫번째,
// size of second vector 275 // 두번째에서 쓰레기 값이 나옵니다.
// size of third vector 0
// size of fourth vector 0
// size of fifth vector 0 // 여긴 오히려 0, 여기에서 쓰레기 값이 나와야 하는거 아닌가요.
Keys k;
remainingSequence[5].push_back(k); // 에러발생 없습니다.
}
- 해당 코드는 큰 파일의 일부 입니다.
- 컴파일시 명령어가 g++-9 -std=c++17 면 에러가 생기고
- 컴파일시 명령어에 최적화 옵션을 넣으면 g++-9 -01 -std=c++17 에러가 사라집니다.
제공된 코드만 컴파일 하여 보면 문제가 없을수 있으나 제가 하고 있는 작업에서는 해당 문제가 생기고 제공된 코드와 실제의 차이는 없다고 봅니다. 스트럭처를 define 후 바로 사용하는데도 문제가 생기는데 이유를 모르겠습니다.
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력