1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
#include <iostream>
#include <limits>
#include <map>
#include <vector>
std::size_t makeAllStringsSameLength(std::vector<std::string> phrase);
int main()
{
std::map<char, int> charintmap;
std::vector<std::vector<int>> myvec;
for(char c='A'; c<='Z'; c++) {
charintmap.emplace(std::make_pair(c, int((c-'A'+1))));
myvec.emplace_back(std::initializer_list<int>{c, 0});
}
for(char c='a'; c<='z'; c++) {
charintmap.emplace(std::make_pair(c, int((c-'a'+'Z'-'A'+2))));
myvec.emplace_back(std::initializer_list<int>{c, 0});
}
for(const auto& couple : charintmap) {
std::cout << "charintmap.at(" << couple.first
<< ") == " << couple.second << '\n';
}
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
for(size_t row{0}; row<myvec.size(); row++) {
std::cout << "myvec.at(" << row << ").at(0) == "
<< char(myvec.at(row).at(0))
<< "; myvec.at(" << row << ").at(1) == "
<< myvec.at(row).at(1) << '\n';
}
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::vector<std::string> words = { "next", "time", "please",
"provide", "a", "compilable",
"piece", "of", "your",
"code", "along", "with",
"examples", "of", "the",
"data", "you", "are",
"using" };
// 'wordlen' would inform you about how long strings are, if needed.
int wordlen = makeAllStringsSameLength(words);
for(const auto& word : words) {
for(const auto& ch : word) {
int index{0};
if('A' <= ch && ch <= 'Z') index = int('Z' - ch);
if('a' <= ch && ch <= 'z') index = int('z' - ch);
myvec.at(index).at(1) += charintmap.at(ch);
}
}
for(size_t row{0}; row<myvec.size(); row++) {
std::cout << "myvec.at(" << row << ").at(0) == "
<< char(myvec.at(row).at(0))
<< "; myvec.at(" << row << ").at(1) == "
<< myvec.at(row).at(1) << '\n';
}
return 0;
}
std::size_t makeAllStringsSameLength(std::vector<std::string> phrase)
{
std::size_t longest{0};
for(auto& word : phrase) {
if(word.length() > longest) {
longest = word.length();
}
}
for(auto& word : phrase) {
std::size_t spaces = longest - word.length();
switch (spaces) {
case 0:
break;
case 1: // 1 / 2 == 0 --> integer division
word.insert(0, 1, ' ');
break;
default:
word.insert(0, spaces/2, ' ');
// now word.length != wlen
word.insert(word.length(), longest-word.length(), ' ');
break;
}
}
for(auto& word : phrase) {
std::cout << word << ": " << word.length() << '\n';
}
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return longest;
}
|