c++ - 使用基于范围的 for 循环以相反的顺序打印 vector

标签 c++ c++11 for-loop binary

我有将十进制数转换为二进制数的代码:

#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 循环打印 vector

例如:十进制 = 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

关于c++ - 使用基于范围的 for 循环以相反的顺序打印 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29046300/

相关文章:

c++ - 搜索/迭代 boost::spirit::qi::symbols

c# - 当缺少所有表达式时,C# for 循环会做什么。例如 (;;) {}

javascript - 动态添加输入类型

python - 什么时候应该使用 Map 而不是 For 循环?

c++ - 如何对 char * 进行深拷贝?

c++ - 在您最喜欢的 C++0x 特性中,您最不喜欢的是什么?

c++ - 在 C++ 中定义全局数据的最佳方式是什么?

减少 CPU 指令大小的 C++ 技术?

c++ - 使用 std::bind 调用 std::string 方法

c++ - 读取(int fd,void *buf,size_t count): prevent implicit conversion