Reputation: 1
I am trying to write a program that takes in user input, store it into a vector of strings, and prints out the vector according to the functions.
for this function, "display_backwards", it is supposed to display the input of the user in a mirrored-like image.
I'm having trouble writing the code and it's giving me errors which I don't understand why
This is my code:
void asci_art::display_backwards(vector<string> art)
{
char swap[100];
cout << "Your Artwork in mirrored image" << endl;
cout << "=============================" <<endl;
for (unsigned int i=0; i < art.size(); i++)
{
for(int j=0; j < art[i].size(); j++)
{
swap[j] = art[i].end()-j;
art[i].begin()+j = swap[j];
}
}
for(int k= 0; k < swap.size(); k++)
{
cout << swap[k];
}
cout << endl;
}
The function is written in a class
The vector, art, has the user input. and for each element of the vector, it stores a line of string. I want to access the string of the element and swap the letters of the string, which i believe would create a mirrored image.
I get compiling errors such as "cannot convert _normal iterator> to char" which i don't understand why because I am dealing with chars, the same type. "no such operation as '='" ??
I am not understanding why. Can someone explain? Maybe my logic is wrong, so can someone help me rewrite it?
I want it to reflect/mirror it vertically.
Upvotes: 0
Views: 1775
Reputation: 3345
Within your code you are mixing the usage of direct array access and iterators. To make your code work as wanted, you would need to decide for one of those.
A second thing is, you try to store the reversed array. Since you do not return the result of that, you would not need to do that. You can just directly output the data in reversed order.
Examples:
// using direct access
void asci_art::display_backwards_direct(std::vector<std::string> art) {
std::cout << "Your Artwork in mirrored image" << std::endl;
std::cout << "=============================" <<std::endl;
for (signed int i = art.size() - 1; i >= 0; --i) {
std::cout << art[i]; // depending on your data you might need endl here
}
std::cout << std::endl;
}
// using iterators
void asci_art::display_backwards_iterators(std::vector<std::string> art) {
std::cout << "Your Artwork in mirrored image" << std::endl;
std::cout << "=============================" <<std::endl;
for (std::vector<std::string>::iterator it = art.rbegin(); it != art.rend(); ++it) {
std::cout << *it; // depending on your data you might need endl here
}
std::cout << std::endl;
}
// using direct access
void asci_art::display_backwards_direct_horizontal(std::vector<std::string> art) {
std::cout << "Your Artwork in mirrored image" << std::endl;
std::cout << "=============================" <<std::endl;
for (signed int i = 0; i < art.size(); ++i) {
std::cout << std::reverse(art[i].begin(), art[i].end()); // depending on your data you might need endl here
}
std::cout << std::endl;
}
// using iterators
void asci_art::display_backwards_iterators_horizontal(std::vector<std::string> art) {
std::cout << "Your Artwork in mirrored image" << std::endl;
std::cout << "=============================" <<std::endl;
for (std::vector<std::string>::iterator it = art.begin(); it != art.end(); ++it) {
std::cout << std::reverse(it->begin(), it->end()); // depending on your data you might need endl here
}
std::cout << std::endl;
}
C++11 syntax would shorten the second version.
Unrelated: using references for passing arguments may speed up the application, when you deal with really vectors, so the vector would not be copied every time.
void some_function(std::vector<std::string> const & const_reference_to_my_string_vector) {
std::cout << const_reference_to_my_string_vector.size() << std:endl;
}
Upvotes: 0
Reputation: 109119
Use std::reverse
to reverse each string in the vector
.
void asci_art::display_backwards(vector<string> art)
{
for( auto&& a : art ) {
std::reverse(a.begin(), a.end()); // reverses each string
std::cout << a << std::endl;
}
}
Or if you want to reverse the order of the strings in the vector, a slightly different call to reverse will do the trick.
void asci_art::display_backwards(vector<string> art)
{
std::reverse( art.begin(), art.end() ); // reverses order of strings in vector
for( auto const& a : art ) {
std::cout << a << std::endl;
}
}
Upvotes: 3
Reputation: 77
The problem is with : art[i].begin()+j=swap[j] what are you trying to do? assigning 'swap[j]' which is a character to an integer?
Upvotes: 0