我创建了一个程序来反转一句话:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string sentence;
string reversedSentence;
int i2 = 0;
cout << "Type in a sentence..." << endl;
getline(cin, sentence);
reversedSentence = sentence;
reverse(reversedSentence.begin(), reversedSentence.end());
cout << sentence << endl;
}
但当我试图用MinGW G++编译它时,会发生这种情况:
SentenceReverser.cpp: In function 'int main()':
SentenceReverser.cpp:16:5: error: 'reverse' was not declared in this scope
16 | reverse(reversedSentence.begin(), reversedSentence.end());
| ^~~~~~~
我不知道我做错了什么。帮助
std::reverse()
在algorithm
标头中定义。包括:
#include <algorithm>
此外,避免using namespace std;
。