Enter any four characters (e.g. abcd) and output them in reverse order (e.g. dcba)
In c++, I tried five solutions:
1. Use arrays directly
#include<iostream>
using namespace std;
int main()
{
char a[4];
cout<<"please input for chars:"<<endl;
for (int i=0;i<4;i++)
{
cin>>a[i];
}
cout<<"reverse output:";
for (int j=3;j>=0;j--)
{
cout<<a[j];
}
cout<<endl;
return 0;
}
2. Use string to implement
String can be seen as a special container. When using string, I am not familiar with it, so I first look up the explanation of string. For string's research, I will continue to write when I have time.
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s;
cout<<"please input a string:"<<endl;
cin>>s;
cout<<"reverse output:";
for(int i=3;i>=0;i--)
{
cout<<s[i];
}
cout<<endl;
return 0;
}
There are three points to be explained here: (1) String in c++ can be accessed by character transmission. (2) String library must be introduced when string is used. (3) If cin is used as input stream, blank space or carriage return can not be accepted, that is to say, only one word can be accepted.
3. Use containers and iterators in stl
Containers: Managing a collection of objects of a certain kind can be understood as containers for data. Each type of data has its own member functions to use. For example, deque (queue), list (linked list), vector (array), map
algorithms: Acts on containers, providing ways to perform various operations. The specific implementation method is already encapsulated, we just need to learn how to call it. For example, initialization, sorting, search, and transformation.
iterators: Used specifically to facilitate object elements in containers.
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<char> list1(4);
cout<<"please input four chars:"<<endl;
for (int i=0;i<4;i++)
{
char a;
cin>>a;
list1.push_back(a);
}
list1.reverse();
cout<<"reverse output:";
for(list<char>::iterator it=list1.begin();it!=list1.end();it++)
{
cout<<*it;
}
cout<<endl;
return 0;
}
In this attempt, I used the reverse member function that comes with the list container.
4. Use containers and algorithms in stl and iterators
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
list<char> list1(4);
cout<<"please input four chars:"<<endl;
for (int i=0;i<4;i++)
{
char a;
cin>>a;
list1.push_back(a);
}
reverse(list1.begin(),list1.end());
cout<<"reverse output:";
for(list<char>::iterator it=list1.begin();it!=list1.end();it++)
{
cout<<*it;
}
cout<<endl;
return 0;
}
The reverse algorithm of the algorithm is used here.
5. Use containers and algorithms in stl and reverse iterators
#include <iostream>
#include <list>
using namespace std;
int main()
{
list<char> list1(4);
cout<<"please input four chars:"<<endl;
for (int i=0;i<4;i++)
{
char a;
cin>>a;
list1.push_back(a);
}
cout<<"reverse output:";
for(list<char>::reverse_iterator r_it=list1.rbegin();r_it!=list1.rend();r_it++)
{
cout<<*r_it;
}
cout<<endl;
return 0;
}
The access mechanism of the reverse iterator is the same as that of the forward iterator, but in the reverse iterator, the last element is regarded as the first element, and in the process of ++, it is in the direction of the first element. With a reverse iterator, you don't have to change the contents of the list.
Next, we need to switch to Python to complete this topic.
Python language has many differences compared with c++. Here, I just compare the contents involved in solving this problem.
(1) Variables do not need to be defined in Python. For example, in traditional C and C++, we need to tell the compiler that the variable we define is an integer, floating-point, or string. I don't need it in Python. Based on the data type of the variable, the interpreter allocates the specified memory and decides what data can be stored in memory.
(2)
1. The first method is slicing.
This method is the simplest in Python.
str = raw_input("please input a string:")
print 'reverse output:\n'
print str[::-1]
2. Direct Reverse Output String: Simulating Slice Operations
str = raw_input("please input a string:")
print 'reverse output:\n'
length=len(str)-1
str1=''
while length>=0:
str1 += str[length]
length -= 1
print str1
In this small code, the following error occurred:
Traceback (most recent call last):
File "C:/Users/Administrator/Desktop/test/python1.py", line 6, in <module>
str1 += str[length]
NameError: name 'str1' is not defined
Because although variables in Python do not need to be declared, if they are not assigned, the output will be wrong, saying that the variable is undefined. So you add strl="and give it a null value.
3. Because there is a reverse method in the list, you can convert strings into lists. If you want to display it as a string after converting to a list, you must use the join function to connect the elements in the list.
str = raw_input("please input a string:")
print 'reverse output:\n'
list1=list(str)
list1.reverse()
str1=''.join(list1)
print str1
If not connected, the output is as follows:
please input a string:123434
reverse output:
['4', '3', '4', '3', '2', '1']