2017-05-05 6 views
-1
#include <iostream> 
#include <vector> 
#include <cstdlib> 
#include <time.h> 

using namespace std; 

void makeVector(); 
void breakVector(); 

vector<char> asciiChar; 
vector<char> shuffledChar; 

int main(){ 
    srand((unsigned) time(NULL)); 
    makeVector(); 
    breakVector(); 
} 

void makeVector(){ 
    for(char i = 32; i < 127; i++){ 
     asciiChar.push_back(i); 
     cout << i << " "; 
    } 
    cout << endl << endl; 
} 

void breakVector(){ 
    for(int i = 0; i < asciiChar.size(); i++){ 
     int j = rand() % asciiChar.size(); 
     shuffledChar.push_back(asciiChar.at(j)); 
     asciiChar[j].erase();     //34 error ******* 
    } 
    for(int i = 0; i < 95; i++){ 
     cout << shuffledChar.at(i) << " "; 
    } 
} 

.C++ 벡터 erase() 오류, 컴파일되지 않음

...|31|warning: comparison between signed and unsigned integer expressions [-Wsign-compare]| 
C:\Users\Owner\Documents\C++\asciiShuffle\main.cpp|34|error: request for member 'erase' in 'asciiChar.std::vector<_Tp, _Alloc>::operator[]<char, std::allocator<char> >(((std::vector<char>::size_type)j))', which is of non-class type '__gnu_cxx::__alloc_traits<std::allocator<char> >::value_type {aka char}'| 
||=== Build failed: 1 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===| 

중복 값을 피하기 위해 다른 벡터에 값을 할당하는 데 사용 된 벡터의 위치를 ​​삭제하려고합니다. 이 코드는 벡터를 작성하고 내용을 다른 내용으로 셔플해야합니다.

다른 프로그램에서 비슷한 기능으로 .erase()를 사용했지만 저에게 효과적 이었지만이 오류 메시지는 이해할 수 없으며 검색 결과는 관련성이 없습니다.

+1

'char's 회원들에게,이없는'삭제()'또는 기타 :

은 당신이 달성하고자하는 것은 아마 이것이다. –

+0

문자 유형 벡터 요소를 삭제할 수 없습니까? – wacey

+0

싫어함. iterator를 통해 그것을 수행하고 벡터 자체에서'erase()'를 호출하십시오 (@ Isuka의 답변 에서처럼). – stefaanv

답변

2
asciiChar[j].erase(); 

erase() 메서드는 벡터 자체가 아니라 char 요소에서 사용하려고합니다.

erase은 벡터 클래스의 한 방법입니다. 따라서 벡터의 요소가 아닌 asciiChar 벡터에 사용해야합니다.

결코 요소를 반복하면서 벡터에서 요소를 지워야한다는 것에 유의하십시오.

while(asciiChar.size() > 0){ 
    int j = rand() % asciiChar.size(); 
    shuffledChar.push_back(asciiChar.at(j)); 
    asciiChar.erase(asciiChar.begin() + j); 
} 
+0

고마워요. 아직 모릅니다. – wacey