converting an array of integers to string

后端 未结 5 1724
闹比i
闹比i 2021-01-25 11:23

If I have an array that looks like

int digits[size] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4}

I want to remove the leading zeros and to d

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-25 11:55

    To answer your actual question (How to remove the leading zeros?) here a solution without strings:

    #include 
    #include 
    #include 
    using namespace std;
    
    int main() {
        std::vector x = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
        // ...or if you insist on the array...
        // int x[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 4};
        // std::vector x = {x,x+15};
        auto it = std::find_if_not(x.begin(),x.end(),[](int i){return i==0;});
        std::vector y{it,x.end()};
        for (auto i : y) std::cout << i << " ";
    }
    

    prints:

    1 2 3 0 4

提交回复
热议问题