是否可以在C+中交换任意大小的数组+;? 我想知道是否可以交换两个不同大小的C++数组的内容(不使用任何预定义的C++函数)?我的代码如下: #include <iostream> #include <string> using namespace std; void swapNames(char a[], char b[]) { //can be done with one temp; using two for clarity purposes char* temp = new char[80]; char* temp2 = new char[80]; int x = 0; while(*(b+x)!='\0') { *(temp+x) = *(b+x); x=x+1; } x=0; while(*(a+x)!='\0') { *(temp2+x) = *(a+x); x=x+1; } x=0; while(*(temp2+x)!='\0') { *(b+x) = *(temp2+x); x=x+1; } x=0; while(*(temp+x)!='\0') { *(a+x) = *(temp+x); x=x+1; } } int main() { char person1[] = "James"; char person2[] = "Sarah"; swapNames(person1, person2); cout << endl << "Swap names..." << endl; cout << endl << "Person1 is now called " << person1; cout << "Person2 is now called " << person2 << endl;; } #包括 #包括 使用名称空间std; 无效交换名称(字符a[],字符b[] { //可以用一个温度来完成;为了清晰起见,使用两个温度 char*temp=新字符[80]; char*temp2=新字符[80]; int x=0; 而(*(b+x)!='\0') { *(温度+x)=*(b+x); x=x+1; } x=0; 而(*(a+x)!='\0') { *(temp2+x)=*(a+x); x=x+1; } x=0; 而(*(temp2+x)!='\0') { *(b+x)=*(temp2+x); x=x+1; } x=0; 而(*(温度+x)!='\0') { *(a+x)=*(温度+x); x=x+1; } } int main() { charperson1[]=“詹姆斯”; charperson2[]=“莎拉”; swapNames(第1人、第2人); 库特
仅是以下内容的简写:是否可以在C+中交换任意大小的数组+;? 我想知道是否可以交换两个不同大小的C++数组的内容(不使用任何预定义的C++函数)?我的代码如下: #include <iostream> #include <string> using namespace std; void swapNames(char a[], char b[]) { //can be done with one temp; using two for clarity purposes char* temp = new char[80]; char* temp2 = new char[80]; int x = 0; while(*(b+x)!='\0') { *(temp+x) = *(b+x); x=x+1; } x=0; while(*(a+x)!='\0') { *(temp2+x) = *(a+x); x=x+1; } x=0; while(*(temp2+x)!='\0') { *(b+x) = *(temp2+x); x=x+1; } x=0; while(*(temp+x)!='\0') { *(a+x) = *(temp+x); x=x+1; } } int main() { char person1[] = "James"; char person2[] = "Sarah"; swapNames(person1, person2); cout << endl << "Swap names..." << endl; cout << endl << "Person1 is now called " << person1; cout << "Person2 is now called " << person2 << endl;; } #包括 #包括 使用名称空间std; 无效交换名称(字符a[],字符b[] { //可以用一个温度来完成;为了清晰起见,使用两个温度 char*temp=新字符[80]; char*temp2=新字符[80]; int x=0; 而(*(b+x)!='\0') { *(温度+x)=*(b+x); x=x+1; } x=0; 而(*(a+x)!='\0') { *(temp2+x)=*(a+x); x=x+1; } x=0; 而(*(temp2+x)!='\0') { *(b+x)=*(temp2+x); x=x+1; } x=0; 而(*(温度+x)!='\0') { *(a+x)=*(温度+x); x=x+1; } } int main() { charperson1[]=“詹姆斯”; charperson2[]=“莎拉”; swapNames(第1人、第2人); 库特,c++,arrays,C++,Arrays,仅是以下内容的简写: char person1[6] = "James"; 您以后不能在person1中存储超过6个字符。如果您真正想要的是长度不同的字符串,我建议放弃C样式的字符串,改用std::string标准库类型: #include <string> #include <algorithm> std::string person1 = "James"; std::string person2 = "Sarah"; swap(person1, person2)
char person1[6] = "James";
您以后不能在person1中存储超过6个字符。如果您真正想要的是长度不同的字符串,我建议放弃C样式的字符串,改用std::string
标准库类型:
#include <string>
#include <algorithm>
std::string person1 = "James";
std::string person2 = "Sarah";
swap(person1, person2);
#包括
#包括
仅是以下内容的简写:
char person1[6] = "James";
您以后不能在person1中存储超过6个字符。如果您真正想要的是长度不同的字符串,我建议放弃C样式的字符串,改用std::string
标准库类型:
#include <string>
#include <algorithm>
std::string person1 = "James";
std::string person2 = "Sarah";
swap(person1, person2);
#包括
#include.只要数组大小固定,就允许引用数组
有一个简单的答案,而不是你正在考虑的所有复杂的事情。只需使用一个向量
vector<char> a;
vector<char> b;
...
a.swap(b);
向量a;
载体b;
...
a、 掉期(b);
还有什么更容易的
向量也是你问题的答案,“也就是说,在没有预先定义大小的情况下,是否可以在C++中创建一个新数组?”你可以创建一个向量,然后再调整它(几乎是同一个东西)。
有一个简单的答案,而不是你正在考虑的所有复杂的事情。只需使用一个向量
vector<char> a;
vector<char> b;
...
a.swap(b);
向量a;
载体b;
...
a、 掉期(b);
还有什么更容易的
向量也是你问题的答案,“也就是说,在没有预先定义大小的情况下,是否可以在C++中创建一个新数组?”你可以创建一个向量,然后再调整它(这几乎是同一个东西)。.
与原始数组相比,您应该始终更喜欢容器类。但是,如果您绝对必须使用数组,您当然可以在不动态分配临时数组的情况下完成交换。使用模板静态确定传递到交换函数中的数组的类型和大小
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
template<typename T, size_t N>
void swap_arrays( T(&left)[N], T(&right)[N] )
{
T temp[N];
// copy left to temp
std::copy( std::begin(left), std::end(left), std::begin(temp) );
// copy right to left
std::copy( std::begin(right), std::end(right), std::begin(left) );
// copy temp to right
std::copy( std::begin(temp), std::end(temp), std::begin(right) );
}
template<typename T, size_t N>
void swap_and_print( T(&left)[N], T(&right)[N] )
{
std::cout << "\nBefore swapping: \n";
std::cout << " Left:";
std::for_each( std::begin(left), std::end(left),
[]( T const& t ) { std::cout << " " << t; } );
std::cout << "\n Right:";
std::for_each( std::begin(right), std::end(right),
[]( T const& t ) { std::cout << " " << t; } );
swap_arrays( left, right );
std::cout << "\nAfter swapping: \n";
std::cout << " Left:";
std::for_each( std::begin(left), std::end(left),
[]( T const& t ) { std::cout << " " << t; } );
std::cout << "\n Right:";
std::for_each( std::begin(right), std::end(right),
[]( T const& t ) { std::cout << " " << t; } );
}
int main()
{
int foo1[] = {1,2,3,4,5};
int bar1[] = {6,7,8,9,10};
swap_and_print( foo1, bar1 );
char foo2[] = "James";
char bar2[] = "Sarah";
swap_and_print( foo2, bar2 );
}
注意:第二个示例末尾的奇怪字符是因为输入是包含终止空字符的char
数组;您看到的是它的视觉表示(因为代码将其打印为字符).与原始数组相比,您应该始终更喜欢容器类。但是,如果您绝对必须使用数组,您当然可以在不动态分配临时数组的情况下完成交换。使用模板静态确定传递到交换函数中的数组的类型和大小
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
template<typename T, size_t N>
void swap_arrays( T(&left)[N], T(&right)[N] )
{
T temp[N];
// copy left to temp
std::copy( std::begin(left), std::end(left), std::begin(temp) );
// copy right to left
std::copy( std::begin(right), std::end(right), std::begin(left) );
// copy temp to right
std::copy( std::begin(temp), std::end(temp), std::begin(right) );
}
template<typename T, size_t N>
void swap_and_print( T(&left)[N], T(&right)[N] )
{
std::cout << "\nBefore swapping: \n";
std::cout << " Left:";
std::for_each( std::begin(left), std::end(left),
[]( T const& t ) { std::cout << " " << t; } );
std::cout << "\n Right:";
std::for_each( std::begin(right), std::end(right),
[]( T const& t ) { std::cout << " " << t; } );
swap_arrays( left, right );
std::cout << "\nAfter swapping: \n";
std::cout << " Left:";
std::for_each( std::begin(left), std::end(left),
[]( T const& t ) { std::cout << " " << t; } );
std::cout << "\n Right:";
std::for_each( std::begin(right), std::end(right),
[]( T const& t ) { std::cout << " " << t; } );
}
int main()
{
int foo1[] = {1,2,3,4,5};
int bar1[] = {6,7,8,9,10};
swap_and_print( foo1, bar1 );
char foo2[] = "James";
char bar2[] = "Sarah";
swap_and_print( foo2, bar2 );
}
注意:第二个示例末尾的奇怪字符是因为输入是包含终止空字符的char
数组;您看到的是它的视觉表示(因为代码将其打印为字符)。我将使用std::string a,b;
和std::swap(a,b)那些不是C++数组,而是C数组。我将使用<代码> STD::string a,b;< /c> >和<代码> STD::SWAP(a,b)我想通过使用字符数组来更好地理解数组如何在C++中工作。在这种情况下,答案是否定的。数组不是C++ C++中的第一类类型(例如,你不能分配它们)。谢谢。)我试图找出是否可以使用char数组来更好地理解数组如何在C++中工作。@ eNeXalvin在这种情况下,答案是否定的。数组不是C/C++中的第一类类型(例如,你不能分配它们)。和个人1[6]这很有帮助。它确实让抽象概念更清晰了。谢谢。关于std::string,我更想弄清楚是否可以使用字符数组进行交换。我开始认为可能不行。@EnsignVariable,如果您想使用数组而不是std::string
use。有一个std::sw的专门化ap
对于此容器,因此可以交换相同大小和类型的2std:array
s。查看您对person1[]和person1[6]之间相似性的说明这很有帮助。它确实让抽象概念更清晰了。谢谢。关于std::string,我更想弄清楚是否可以使用字符数组进行交换。我开始认为可能不行。@EnsignVariable,如果您想使用数组而不是std::string
use。有一个std::sw的专门化ap
对于此容器,因此可以交换相同大小和类型的2std:array
s。感谢向我介绍模板:)尽管如此,我还是更倾向于尽可能多地使用数组。尽管如此,很酷的实现。感谢向我介绍模板:)我更倾向于尽可能多地使用数组,尽管尽管如此,还是很酷。