我已經爲此代碼編寫了主幹。我只需要一點關於我如何完成這些功能的見解。我認爲a.swap(b)
可以用於交換同一陣列中的兩個字符串。我錯了嗎?C++:數組中的反向字符串。在兩個不同的陣列之間交換字符串
任何見解/建議表示讚賞。
#include <string>
using std::string;
#include <iostream>
#include <cassert>
using namespace std;
void swap(string & a, string & b); // swaps two strings.
void reverse_arr(string a1[], int n1); // reverse an array of strings.
void swap_arr(string a1[], int n1, string a2[], int n2); // swaps two arrays of strings.
int main(){
string futurama[] = { 「fry」, 「bender」, 「leela」,
「professor farnsworth」, 「amy」,
「doctor zoidberg」, 「hermes」, 「zapp brannigan」,
「kif」, 「mom」 };
for (int i=0;i<10;i++)
cout << futurama[i] << endl;
swap(futurama[0],futurama[1]);
cout << 「After swap(futurama[0],futurama[1]);」 << endl;
for (int i=0;i<10;i++)
cout << futurama[i] << endl;
reverse_arr(futurama,10);
cout << 「After reverse_arr(futurama,10);」 << endl;
for (int i=0;i<10;i++)
cout << futurama[i] << endl;
// declare another array of strings and then
// swap_arr(string a1[], int n1, string a2[], int n2);
char w;
cout << 「Enter q to exit.」 << endl;
cin >> w;
return 0;
}
void swap(string & a, string & b){
// swaps two strings.
a.swap(b);
}
void reverse_arr(string a1[], int n1) {
// Reverse an array of strings.
}
void swap_arr(string a1[], int n1, string a2[], int n2) {
// swaps two arrays of strings.
}
縮進,請! – 2012-03-09 00:58:41
看看http://www.cplusplus.com/reference/string/swap/ – noMAD 2012-03-09 01:03:42
如果「我是我錯了嗎?」是你的全部問題,這可能會被關閉。你有這個代碼的特定問題嗎?它工作嗎?如果沒有,具體是什麼不做你期望的?我們非常樂意幫助您做家庭作業,但您至少應該提出有意義的問題。一個模糊的「我錯了嗎?」沒有其他東西沒有幫助(或者試圖自己解決它的跡象)。 – 2012-03-09 01:11:29