2017-01-18 4 views
0

일부 코드를 코딩하고 있었고 문제가 발생했습니다. 어떻게 큐에 배열을 삽입합니까? 하는 array1의 모든 요소를 ​​인쇄해야합니다 .... 제가 예하여 설명 제공배열을 대기열에 저장하는 방법은 무엇입니까?

array1[9]={1,3,4,2,1,2,3,4,1} 
array2[9]={2,3,4,2,4,2,2,2,3} 

//create a queue (I don't know how to declare a queue for this issue)... 

queue.push(array1); 
queue.push(array2); 
//############################## 
//when I print queue.front()... 
//######################## 
printf("%d",queue.front()); 

위의 조각을하자! 어떻게 할 수 있니?

+2

이 질문에는 'c' 또는'C++ '중 하나를 선택해야한다고 생각합니다. 둘 다 꽤 다른 언어입니다. – RoadRunner

+1

큐 튜토리얼을 보시오 : http://www.dreamincode.net/forums/topic/57487-stl-queues/ – DyZ

+4

'큐 < vector>'을 사용하지 않을까요? –

답변

1

:-) 당신의 마음에 드는 검색 엔진에 정확히를 검색 할 수 있습니다.

std::array<int, 9> array1 = { 1, 3, 4, 2, 1, 2, 3, 4, 1 }; 
std::array<int, 9> array2 = { 2, 3, 4, 2, 4, 2, 2, 2, 3 }; 

std::queue<std::array<int, 9>> queue; 

queue.push(array1); 
queue.push(array2); 

for (auto i : queue.front()) { 
    std::cout << i; 
} 
+0

감사합니다. 요셉 !! ,하지만 만약 내가 front array1을 터뜨리 길 원한다면 똑같은가? Queue.pop()을 작성하면 대기열에서 array1이 제거되어야합니다 !! 위와 동일합니까 ?? –

+0

@BharathM 아니오'pop()'은 요소를 제거하지만'front()'는 요소를 제거하지 않습니다. 'front()'는 요소에 대한 참조만을 반환합니다. –

+0

@BharathM이 답변에 무엇이 잘못 되었습니까? –

0

이 질문에 대한 대답은 너무 간단합니다. 동적 배열의 크기를해야하는 경우, 당신이 대신 C 스타일 배열의 std::array를 사용하는 경우 그것은 쉽게

int array1[9]={1,3,4,2,1,2,3,4,1}; 
int array2[9]={2,3,4,2,4,2,2,2,3}; 
std::vector<int> a1(9); 
std::vector<int> a2(9); 
for(int i =0; i< 9;i++){   
    a1[i]=array1[i]; 
    a2[i]=array2[i];// <= a2[i] should take values from array2[i] 
} 
std::queue<std::vector<int>> queue;  
queue.push(a1); 
queue.push(a2); 
//std::cout<<queue.size()<<std::endl; 
std::vector<int> temp(9); 
int size =queue.size(); 
for(int i =0; i< size;i++){   
    temp=queue.front(); 
    std::cout<<"Array["<<i<<"]: "; 
    for(int j =0; j< temp.size();j++) 
     std::cout<<temp[j]<<","; 
    std::cout<<std::endl; 
    queue.pop(); 
} 
관련 문제