Vector of strings to bitset
vector<string> alphabet;
alphabet.push_back("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
vector<bitset<7>> bset(alphabet.size());
for (int c = 0; c < alphabet.size(); c++)
bset[c] = alphabet[c];
for (int c = 0; c < alphabet.size(); c++)
{
cout << alphabet[c] << '\t';
cout << bset[c] << '\t';
cout << hex <<int(alphabet[c]) << '\t';
cout << dec << int(alphabet[c]) << endl;
}
A vector of strings that you push one string into has a size of 1. I think you just want a string (a "vector of chars", as it were).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
#include <iostream>
#include <iomanip>
#include <string>
#include <bitset>
using namespace std;
int main() {
string alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
vector<bitset<7>> bset(alphabet.size());
for (size_t c = 0; c < alphabet.size(); c++)
bset[c] = alphabet[c];
for (size_t c = 0; c < alphabet.size(); c++) {
cout << alphabet[c] << " ";
cout << bset[c] << " ";
cout << hex << setfill('0') << setw(2) << int(alphabet[c]) << " ";
cout << dec << setfill(' ') << setw(2) << int(alphabet[c]) << '\n';
}
}
|
If you're just printing the bit patterns, you don't need the vector of bitsets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
#include <iomanip>
#include <string>
#include <bitset>
using namespace std;
int main() {
string alphabet("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
cout << uppercase;
for (size_t c = 0; c < alphabet.size(); c++) {
cout << alphabet[c] << " ";
cout << bitset<7>(alphabet[c]) << " ";
cout << hex << setfill('0') << setw(2) << int(alphabet[c]) << " ";
cout << dec << setfill(' ') << setw(2) << int(alphabet[c]) << '\n';
}
}
|
Last edited on
Topic archived. No new replies allowed.