不确定我是否正确行事。但我尝试显示输出for x, frame in frames.items():
more unrelated code
for x, highc in highcs.items():
而不是cat the sideways ran
。
the cat ran sideways
不确定我是否正确地做了。
答案 0 :(得分:0)
您应该通过引用传递字符串,因为您正在改变它们。编写代码的方式是,您要发送要交换的字符串的临时副本,实际上是交换副本,而不是将它们交换到数组中。更新的代码:
#include <string>
#include <iostream>
using namespace std;
void doSwap(string string1, string string2);
int main() {
string theArray[] = {"the", "cat", "ran", "sideways"};
int i;
for (i = 0; i < (sizeof theArray)/sizeof(string); i++) {
doSwap(theArray[i], theArray[i+1]);
cout << theArray[i] << " ";
}
}
void doSwap (string &string1, string &string2) {
string temp;
temp = string1;
string1 = string2;
string2 = temp;
}
答案 1 :(得分:0)
您可以使用<algorithm>
库轻松完成任务:
int main()
{
vector<string> vec { "the", "cat", "ran", "sideways" };
swap_ranges(begin(vec), begin(vec)+1, begin(vec)+1);
swap_ranges(begin(vec)+2, begin(vec)+3, begin(vec)+3);
}
你不需要让自己变得困难。
答案 2 :(得分:0)
试试这段代码,这很简单:
#include <string>
#include <iostream>
using namespace std;
void doSwap (int first, int last);
string theArray[5] = {"the", "cat", "ran", "sideways"};
int main() {
int i;
for (i = 2; i <=4; i += 2) {
doSwap((i-2),(i-1));
}
for(i=0;i<5;i++)
cout << theArray[i] << " ";
}
void doSwap (int first, int last){
string tmp;
tmp = theArray[first];
theArray[first] = theArray[last];
theArray[last] = tmp;
}