使用基于范围的 for 循环以相反的顺序打印矢量
Printing vector in reverse order using range-based for loop
我有将十进制数转换为二进制数的代码:
#include <iostream>
#include <windows.h>
#include <vector>
#include <algorithm>
using namespace std;
void space(){ cout << endl << endl << endl; }
int main(int argc, char const *argv[]){
system("color 1F");
unsigned long int n, copy_n;
vector<int> v;
cout << "# Decimal: "; cin >> n; cout << endl; copy_n = n;
while (n != 0){
if (n % 2 == 0){ v.push_back(0); }else{ v.push_back(1); }
n = int(n / 2);}
cout << endl << "# Binary: ";
reverse(v.begin(), v.end());
for (size_t i = 0; i < v.size(); i++){cout << v.at(i);}
space(); system("Pause"); return 0;
}
...还有这个...
#include <iostream>
#include <windows.h>
using namespace std;
void space(){ cout << endl << endl << endl; }
int main(int argc, char const *argv[]){
system("color 1F");
unsigned long int n, copy_n, nr = 0 ;
cout << "# Decimal: "; cin >> n; copy_n = n; cout << endl;
while (copy_n != 0){ nr++; copy_n = int(copy_n / 2); }
int* v = new int[nr];
for (int i = 0; i < nr; i++){if (n % 2 == 0){ v[i] = 0; }else{ v[i] = 1; }n = int(n / 2);}
cout << endl << "# Binary: ";
for (int i = nr-1; i >= 0;i--){cout << v[i] << "";}
space(); system("Pause"); return 0;}
...还有这个...
#include <iostream>
#include <windows.h>
#include <bitset>
using namespace std;
void space(){ cout << endl << endl << endl; }
int main(int argc, char const *argv[]){
system("color 1F");
unsigned int n;
cout << "# Decimal: "; cin >> n; cout << endl;
bitset<16>binary(n);
cout << endl << "# Binary: " << binary << endl;
space(); system("Pause"); return 0;
}
但我的问题是:
如何使用算法中的 reverse() 函数并使用基于范围的 for 循环打印向量
例如:十进制 = 2
和
reverse(v.begin(), v.end());
for (size_t i = 0; i < v.size(); i++){cout << v.at(i);}
程序打印 10
和
reverse(v.begin(), v.end());
for(auto i : v){cout<<v.at(i);}
节目原件 01
为什么?我该如何解决这个问题?
这个for语句
reverse(v.begin(), v.end());
for(auto i : v){cout<<v.at(i);}
完全错误。
有效代码如下所示
reverse(v.begin(), v.end());
for(auto i : v){ cout << i;}
此外,如果您根据在源代码中输入的符号数量付费,那么此声明
if (n % 2 == 0){ v.push_back(0); }else{ v.push_back(1); }
看起来很好,因为它包含很多符号。否则最好写成
v.push_back( n % 2 );
同样在您的一个程序中,您动态分配了一个数组
int* v = new int[nr];
但没有释放它。在这种情况下,使用智能指针 std::unique_ptr
.
会更好
你也可以尝试写一个递归函数。例如
#include <iostream>
#include <vector>
std::vector<unsigned int> & dec_to_bin( std::vector<unsigned int> &v,
unsigned int x )
{
const unsigned int Base = 2;
static size_t n;
unsigned int digit = x % Base;
++n;
if ( x /= Base )
{
dec_to_bin( v, x );
}
else
{
v.reserve( n );
n = 0;
}
v.push_back( digit );
return v;
}
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0-exit): ";
unsigned int x = 0;
std::cin >> x;
if ( !x ) break;
std::vector<unsigned int> v;
dec_to_bin( v, x );
for ( auto digit : v ) std::cout << digit;
std::cout << std::endl;
}
return 0;
}
如果顺序进入
15
7
3
1
0
那么程序输出将是
Enter a non-negative number (0-exit): 15
1111
Enter a non-negative number (0-exit): 7
111
Enter a non-negative number (0-exit): 3
11
Enter a non-negative number (0-exit): 1
1
Enter a non-negative number (0-exit): 0
我有将十进制数转换为二进制数的代码:
#include <iostream>
#include <windows.h>
#include <vector>
#include <algorithm>
using namespace std;
void space(){ cout << endl << endl << endl; }
int main(int argc, char const *argv[]){
system("color 1F");
unsigned long int n, copy_n;
vector<int> v;
cout << "# Decimal: "; cin >> n; cout << endl; copy_n = n;
while (n != 0){
if (n % 2 == 0){ v.push_back(0); }else{ v.push_back(1); }
n = int(n / 2);}
cout << endl << "# Binary: ";
reverse(v.begin(), v.end());
for (size_t i = 0; i < v.size(); i++){cout << v.at(i);}
space(); system("Pause"); return 0;
}
...还有这个...
#include <iostream>
#include <windows.h>
using namespace std;
void space(){ cout << endl << endl << endl; }
int main(int argc, char const *argv[]){
system("color 1F");
unsigned long int n, copy_n, nr = 0 ;
cout << "# Decimal: "; cin >> n; copy_n = n; cout << endl;
while (copy_n != 0){ nr++; copy_n = int(copy_n / 2); }
int* v = new int[nr];
for (int i = 0; i < nr; i++){if (n % 2 == 0){ v[i] = 0; }else{ v[i] = 1; }n = int(n / 2);}
cout << endl << "# Binary: ";
for (int i = nr-1; i >= 0;i--){cout << v[i] << "";}
space(); system("Pause"); return 0;}
...还有这个...
#include <iostream>
#include <windows.h>
#include <bitset>
using namespace std;
void space(){ cout << endl << endl << endl; }
int main(int argc, char const *argv[]){
system("color 1F");
unsigned int n;
cout << "# Decimal: "; cin >> n; cout << endl;
bitset<16>binary(n);
cout << endl << "# Binary: " << binary << endl;
space(); system("Pause"); return 0;
}
但我的问题是:
如何使用算法中的 reverse() 函数并使用基于范围的 for 循环打印向量
例如:十进制 = 2
和
reverse(v.begin(), v.end());
for (size_t i = 0; i < v.size(); i++){cout << v.at(i);}
程序打印 10
和
reverse(v.begin(), v.end());
for(auto i : v){cout<<v.at(i);}
节目原件 01
为什么?我该如何解决这个问题?
这个for语句
reverse(v.begin(), v.end());
for(auto i : v){cout<<v.at(i);}
完全错误。
有效代码如下所示
reverse(v.begin(), v.end());
for(auto i : v){ cout << i;}
此外,如果您根据在源代码中输入的符号数量付费,那么此声明
if (n % 2 == 0){ v.push_back(0); }else{ v.push_back(1); }
看起来很好,因为它包含很多符号。否则最好写成
v.push_back( n % 2 );
同样在您的一个程序中,您动态分配了一个数组
int* v = new int[nr];
但没有释放它。在这种情况下,使用智能指针 std::unique_ptr
.
你也可以尝试写一个递归函数。例如
#include <iostream>
#include <vector>
std::vector<unsigned int> & dec_to_bin( std::vector<unsigned int> &v,
unsigned int x )
{
const unsigned int Base = 2;
static size_t n;
unsigned int digit = x % Base;
++n;
if ( x /= Base )
{
dec_to_bin( v, x );
}
else
{
v.reserve( n );
n = 0;
}
v.push_back( digit );
return v;
}
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0-exit): ";
unsigned int x = 0;
std::cin >> x;
if ( !x ) break;
std::vector<unsigned int> v;
dec_to_bin( v, x );
for ( auto digit : v ) std::cout << digit;
std::cout << std::endl;
}
return 0;
}
如果顺序进入
15
7
3
1
0
那么程序输出将是
Enter a non-negative number (0-exit): 15
1111
Enter a non-negative number (0-exit): 7
111
Enter a non-negative number (0-exit): 3
11
Enter a non-negative number (0-exit): 1
1
Enter a non-negative number (0-exit): 0