PTA - String rotate left (20 minutes c ++ STL)

Enter a string and a non-negative integer N, the string loop in claim left N times.

Input formats:

A given input does not exceed 100 characters in length, to enter the end of the non-empty string in the first line; the line 2 gives the non-negative integer N.

Output formats:

In line output cycle N times left the string.

SAMPLE INPUT

Hello World!
2

Sample output:

llo World! I

The meaning of the title is the first character to move to the beginning of the end, the number is N;
understand the meaning of the questions, it is like the problem-solving
ideas: the first character is extracted, and then deleted, inserted into the end of it OK;

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    string s;
    getline(cin,s);  //输入已回车结束的字符串
    int n;cin>>n;
    char ch;
    while(n--)  //要移动的次数
    {
        ch=*s.begin();  //因为s.begin() 是迭代器,相当于地址,所以要加'*' 指针
        s.erase(s.begin());  //删除迭代器 s.begin() 处的元素,这里是第一个元素
        s.push_back(ch);  //末尾插入
    }
    for(string::iterator it=s.begin();it!=s.end();it++)  //迭代器输出
        cout<<*it;
    system("pause");
	return 0;
}

You can leave a little thing like like

Released four original articles · won praise 1 · views 76

Guess you like

Origin blog.csdn.net/weixin_45836300/article/details/104347552