Vector<bool> wyrzuca blad przy przypisywaniu

0

Czesc. Mam tutaj maly programik ktory powinien zbierac 8 liczb 0 lub 1 i przeprowadzac inkrementacje binarna. Problem w tym ze program po kompliacji wyrzuca blad numer 3, w funkcji readdata.

#include <iostream>
#include <vector>

using namespace std;

void inc(vector<bool> num, int elem){
	if (num[elem]==false){
		num[elem]=!num[elem];
		return;
	}
	if (num[elem]==true){
		num[elem]=!num[elem];
		inc(num, elem+1);
		return;
	}
}

vector<bool> readdata(){
	char c;
	vector<bool> ans;
	for (int i = 0;i<8;++i){
		cin >> c;
		switch (c){
		case '1':
			ans[i]=true;
		case '0':
			ans[i]=false;
		default:
			ans[i]=false;
		}
	}
	return ans;
}

int main(){
	vector<bool> data = readdata();
	inc(data, 0);
	for (int i = 0;i<8;++i)
		cout << data[i] << ' ';
	char ch;
	cin >> ch;
	return 0;
}
 

Wie ktos od czego to zalezy?

0

Twój wektor jest pusty, a ty się do elementów chcesz odwoływać

0

Wow...

0

Tak myśle, czy nie lepiej użyć zwykłego unsigned char i operatorów bitowych?

2

std::bitset<int>

0

The vector class template has a special template specialization for the bool type.

This specialization is provided to optimize for space allocation: In this template specialization, each element occupies only one bit (which is eight times less than the smallest type in C++: char).

Źródło//www.cplusplus.com/reference/stl/vector/

1 użytkowników online, w tym zalogowanych: 0, gości: 1