Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I want to compare two vectors string
vector <string> morse ={"A.-","B-...","C-.-.", "D-..", "E.", "F..-.", "G--.", "H....", "I.." ,"J.---", "K-.-", "L.-..", "M--" ,"N-." ,"O---" ,"P.--.", "Q--.-", "R.-.", "S...", "T-", "U..-", "V...-", "W.--" ,"X-..-" ,"Y-.--", "Z--.."};
vector<string> codeMorse (1);
codeMorse ={".---.--.-.-.-.---...-.---."};
if (morse[i][j]==codeMorse[k]){ //my problem here =error
}
Can anybody help me?
Your code has 2 problems:
You can't make 2-Dimensional vector that way nor you even tried to make it 2D.
You wrote morse[i][j] without previous definition of i and j.
To fix issue 1 & 2:
Include
#include <vector>
Make a vector of std::pair(s):
std::vector<std::pair<std::string, std::string>> morse;
That allows you to have a pair of strings.
To add a new morse code, use this:
morse.push_back(std::pair<std::string, std::string>("LETTER HERE", "MORSE CODE HERE"));
To read 'em use this:
//read all via loop
for (int i = 0; i <= morse.size(); i++) {
std::cout << "Letter: " << morse[i].first << std::endl; //.first access your first elemt of the pair
std::cout << "Morse Code: " << morse[i].second << std::endl; //.second to access the morse code
}
OR use iterators if you already know them:
//read all via loop
for (auto i = morse.begin(); i != morse.end(); i++) {
std::cout << "Letter: " << i->first << std::endl; //->first access your first elemt of the pair
std::cout << "Morse Code: " << i->second << std::endl; //->second to access the morse code
}
Of course you can read specific values:
std::cout << morse[0].first << std::endl; //[] same use as the array's brackets
std::cout << morse[0].second << std::endl; //same here
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 days ago.
Improve this question
I have an Eigen::MatrixXd and I would like to take all the elements in the matrix to update an attribute // within the class. However when I run the following I get errors.
#include <cmath>
#include <iostream>
#include <Eigen/Core>
double x = 0;
void Increment(double x, double increment)
{
value = value + x + increment;
}
int main()
{
Eigen::MatrixXd m(2, 2);
m << 1, 1, 1, 1;
std::cout << m << std::endl << "becomes: ";
std::cout << std::endl << m.unaryExpr(&Exp(1)) << std::endl;
std::cout << value << std::endl << " is the new value";
}
Expecting to get a value of "8" for last cout statement
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a vector of string including :- chemistry physics maths.
I want to access first character of each word ie c of chemistry p of physics and m of maths. How to do that?
You can output the first index element through this process.
I have made a 2D vector and applied a for loop so each row of the vector's first element is printed.
#include <iostream>
#include <vector>
int main()
{
std::vector<std::string> vec = {"chemistry", "maths", "physics"};
for(int i=0;i<vec.size();i++)
{
std::cout << vec[i][0];
}
return EXIT_SUCCESS;
}
You can also use a range based for loop
for (auto &i : vec)
std::cout << i[0] << " ";
The output will be
c m p
You can treat it like a 2D matrix of characters.
Given the vector of strings:
std::vector<std::string> vec = {"chemestry", "physics", "math"};
You can use a normal loop to access all first characters:
for (int i = 0; i < vec.size(); i++)
std::cout << vec[i][0] << " ";
Or a range based loop:
for (auto &str : vec)
std::cout << str[0] << " ";
Output:
c p m
I think this code should do the trick. If not then the Compiler is being a racist and doesn't like you that much. In that case you can just go-to your old buddy cpp.sh :D
String is a char Array and you can access it's contents from indexes like so
std::string str = "Hello";
std::cout << "First Index: " << str[0];
Output:-
H
Same goes for the vector as well
std::vector <char> str = "World!";
std::cout << "First Index: " << str[0];
Output:-
W
Now if you combine those two, it makes it a 2D Array so you have to access it like you access data from a 2D Array/Matrix.
std::vector <std::string> str = {"Hello", "World", "!"};
std::cout << "First Index Of Element 1: " << str[0][0] << std::endl
<< "First Index Of Element 2: " << str[1][0] << std::endl
<< "First Index Of Element 3: " << str[2][0] << std::endl;
Output:-
H
W
!
Program:-
#include <iostream>
#include <vector>
int main() {
std::vector <std::string> vec = {"chemistry", "physics", "math"};
for (int i=0; i < vec.size(); i++) { //-- size(); Function gives the size of a vector
std::cout << vec[i][0] << std::endl;
}
return 0;
}
Output:-
c
p
m
Press any key to continue...
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
Debugger sais there is an incompatible iterator.
How i can solve this.
What can cause the problem.
Here is my main code
for (std::list<std::chrono::duration<double, std::milli>>
::iterator it = road.get_times().begin()
;it!= road.get_times().end();it++,z++) //error incompatible iterator
{
*road::file << "Samochod z drogi " << road.get_lane_number() << " pojechal ";
switch (road.get_direction())
{
case'S':
*road::file << "prosto" << std::endl;
break;
case'L':
*road::file << "w lewo" << std::endl;
break;
case'R':
*road::file << "w prawo" << std::endl;
break;
}
*road::file << "Jego czas stania w kolejce wyniosl ";
avarage_time += it->count() / 1000;
*road::file << round(it->count() / 1000) << std::endl;
}
function road.get_times ()
std::list< std::chrono::duration<double, std::milli>> get_times()
{
return times;
}
get_times returns by value, which means that every time you call it you get a new list object. This is what happens in your for loop. You call it 2 times and you get two objects. You can't compare iterators from two different objects. To fix this create one object by calling get_times just once:
auto times = road.get_times();
for (auto it = times.begin(); it != times.end(); ++it, z++)
//...
You also might want to pause and consider if returning by value is the right approach. I can't answer that for you since I don't know what times is and what is the structure of your program.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have to use the struct mov
struct mov {
string src;
string dst;
};
where src is the source and dst is the destination. The purpose of the program is to analyze the pieces on a chessboard and generate all of the possible moves. The possible moves must be represented in a set but it must be a set of moves, so set. I've found some methods saying to implement a comparator but I have no clue if it works because when printing the set (using an iterator) I get errors because of the "<<" when printing I guess its conflicting with the comparator since it uses "<"???
<< and < are never confused. Packing mov members and using the fact that std::tuple implements operator< as a lexicographical ordering, you can easily write a comparator of mov as follows:
struct mov
{
std::string src;
std::string dst;
bool operator<(const mov& rhs) const {
return std::tie(src, dst) < std::tie(rhs.src, rhs.dst);
}
};
Then this works with std::set as follows. DEMO is here.
int main()
{
std::set<mov> moves{ {"src1","dts1"}, {"src2","dts2"}, {"src3","dts3"} };
// iterator
std::cout << "Using iterator," << std::endl;
for(auto it = moves.begin(); it != moves.cend(); ++it){
std::cout << it->src << "," << it->dst << std::endl;
}
std::cout << std::endl;
// range-based-for
std::cout << "Using range-based-for," << std::endl;
for(const auto& mov_i : moves){
std::cout << mov_i.src << "," << mov_i.dst << std::endl;
}
return 0;
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I have created a map like this : std::map<int, std::string> mapID;
INPUT: mapID[1] = "string!";
and I want to print element with the key is the s.
For example
cout << "The string is : " << mapID.at(s)->second << endl;
OR
cout << "The string is : " << mapID.find(s)->second << endl;
But I have the error
error: no matching function for call to ‘std::map >::at(std::string&)’
UPDATE:
while(getline(file,s))
{
cout<< s << endl;
number = atoi(s.c_str());
cout << "The string is: " << mapID.at(number)<< endl;
}
If you look at the reference http://en.cppreference.com/w/cpp/container/map/at you will see, that method "at" returns a second element of pair. In your case it is std::string.
The code should be
cout << "The string is : " << mapID.at(1) << endl;
Since C++11, .at() returns a reference to the mapped value of the element identified with key k.
Your example was good if you used an iterator as you can then access your pair element made by your map.
Here is an example with an iterator
auto it = mapID.find(1);
cout << "The string is : " << it->second << endl;
OR with .at()
cout << "The string is : " << mapID.at(1) << endl;
But beware that .at() will throw if the element is not found in your map as it can't create it like the [] operator