How to Iterate over a map <string, vector<string>> m - visual-c++

I need to write a table from a file in a map > m.
I have 3 strings (state, next_states, outputs)
and i want to write them like this (I know it's not correct)
for (i=0; i < 5; i++)
{
//here I have a code, the 3 strings change for every line
for(j=0; j<m.size(); j++)
{
m[i][j] = "state" + "next_states" + "outputs";
}
}
I thought maybe with iterator it would be better but i don't know how to do it.

Here is how you can iterate through your map:
#include <iostream>
#include <map>
#include <vector>
#include <string>
int main()
{
std::map <std::string, std::vector<std::string>> m =
{
{"1", {"a", "b"}},
{"2", {"c", "d", "e"}},
};
for (auto& element : m)
{
std::cout << element.first << " : ";
for (auto& str : element.second)
{
std::cout << str << "; ";
}
std::cout << std::endl;
}
}

Related

Microsoft's code is not working in praxis

This code, under msvc 2022:
const char* chars = "XBECEDX";
for each (char c in chars)
{
if (c == 'X') std::cout << 'A';
else
std::cout << c;
}
Ends with error E0125, E0065, E0029
Maybe output is: ABECEDA
Where is the Failure? Is it bad code or bad compiler or compiler setting or any Failure.
I solve this as vector with lambda function. Here is example:
string s = "XBECEDX";
for_each(s.begin(), s.end(), [](char ch)
{
if (c == 'X') std::cout << 'A';
else std::cout << c;
});
Why not this:
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
string chars = "XBECEDX";
// char chars[] = "XBECEDX"; // will also work as an alternate to string
for (char c : chars)
{
if (c == 'X') std::cout << 'A';
else
std::cout << c;
}
return 0;
}

Complementary DNA(C++)

Task:Write a code to the new string of Dna According to its pattern. Just so you know In DNA strings, symbols "A" and "T" are complements of each other, as "C" and "G".
Fore example:DNA_strand ("ATTGC") //returns "TAACG" or DNA_strand ("GTAT") //returns "CATA"
My Code=>
#include <string>
#include <vector>
#include <iostream>
std::string DNAStrand(const std::string& dna)
{
std::string Sym;
std::string c;
std::stringstream s;
s << Sym;
s >> c;
for(int i = 0; i < dna.size() - 1; i++) {
switch (dna[i]) {
case ('A'):
Sym[i] = 'T';
break;
case ('T'):
Sym[i] = 'A';
break;
case ('C'):
Sym[i] = 'G';
break;
case ('G'):
Sym[i] = 'C';
break;
default:
std::cout << "invalid";
} return c.str();
}
int main() {
std::cout << DNAStrand("ATTGC") << "\n"; //retun "TAACG"
std::cout << DNAStrand("GTAT") << "\n"; //retun "CATA"
}
}
You have created a vector<string>, but in the if statements, you are setting the elements of the vector to chars. You want to build a string, not a vector<string>.
You should replace subsequent if statements with else if, or use a switch statement. Otherwise if statements subsequent to a satisfied if statement are executed needlessly.
Replace this vector with an ostringstream. Naming the stream as s, you would append a char named c with s << c. At the end of iterating over dna, return s.str().

How to print eight words to a line using vector<string>?

I was trying to make a program write 8 words to a line after a user enter their sentence.Its only printing words that have been typed in and i don't have a clue how to make it type 8 words to a line.
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;
vector<string> sentence;
string sente = "";
void print(string, string);
template<typename T>
void print(vector<T>& v, string)
{
cout << "Enter your sentence " << endl;
getline(cin, sente);
sentence.push_back(sente);
for (auto const elem: sentence)
{
cout << elem;
}
}
int main()
{
print(sentence,sente);
}
Using global variables is generally not a good practice.
Also you don't need a extra vector for your use case.
Take a look at the following code, where you can smartly make use of istringstream for your use case:
#include <iostream>
#include <string>
#include <sstream>
void print()
{
std::string sente;
std::cout << "Enter your sentence " << std::endl;
getline(std::cin, sente);
// Used to split string around spaces.
std::istringstream ss(sente);
int wordCountPerLine = 0;
int requiredWordsPerLine = 8;
// Traverse through all words
do {
// Read a word
std::string word;
ss >> word;
// Print the read word
std::cout << word << " ";
wordCountPerLine++;
if(wordCountPerLine % requiredWordsPerLine == 0){
std::cout<<std::endl;
wordCountPerLine = 0;
}
// While there is more to read
} while (ss);
}
int main()
{
print();
}
Feel free to ask any doubts.

Looping back again to Start

now I'm Having problem in repeating the loop after it finished doing the first and i want to try it again without exiting the program? I've been using while loop to do it but still no joy. so i decided to do the if statement. But the Array only accept 4 strings then it exit. Any one who can help? TIA.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
template <typename T>
void GetContents(T& Input);
template <typename T>
void DisplayContents(const T& Input);
int main()
{
int PASS = 0;
// To Display the unsorted and sorted Book Titles
std::vector<std::string> books;
GetContents(books);
std::cout << "\nMy original library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::sort(books.begin(), books.end());
std::cout << "\nMy sorted library (number of books: " << books.size() << "):\n\n";
DisplayContents(books);
std::cout << "Press 1 to try again, else to quit: ";
std::cin >> PASS;
std::cout << "\n";
if (PASS == 1)
{
GetContents(books);
}
else
{
return 0;
}
// to input All book titles
template <typename T>
void GetContents(T& Input)
{
const int MAX = 5;
string bookName;
std::cout << "Enter a Book Titles:\n> ";
for (int i = 0; i < MAX; i++)
{
std::getline(std::cin, bookName);
Input.push_back(bookName);
std::cout <<">";
}
}
//Display All input book titles
template <typename T>
void DisplayContents(const T& Input)
{
for (auto iElement : Input)
{
std::cout << iElement << '\n';
}
std::cout << '\n';
system("pause");
}

Dynamic array with variable sized columns

I am reading a file which has variable sized columns:
0 1 3 0
0 2 0
0 4 0
My code reads the file, but after the last "0" it outputs two numbers which are garbage. Output is: 0130020040-8457888-85648454 (something like that): Please Help me THANK YOU
int **routes = new int *[3]; //create route matrix
for(int i=0;i<3;i++)
{
routes[i] = new int[sizeof(routes)];
}
ifstream routefile;
routefile.open("Sweep_routes.txt");
if(routefile.fail()){
cout << "ERROR";
exit(1);}
for (int i=0;i<3;i++){
for (int j=0;j<sizeof(routes);j++){
routefile>>routes[i][j]; //read
}
}
for (int i=0;i<3;i++){
for (int j=0;j<sizeof(routes);j++){
cout << routes[i][j]; //display
}
}
system("PAUSE");
return 0;
}
You can use this alternative to your code:
#include <iostream>
#include <string>
#include <array>
#include <fstream>
int main()
{
std::array<std::array<std::string, 3>, 3> routes;
std::ifstream routefile("Sweep_routes.txt");
if (routefile)
{
for (auto x : routes)
{
for (auto y : x)
{
if (routefile >> y)
std::cout << y;
}
}
}
std::cin.get();
}

Resources