0

I want to separate in this way a -> num and b -> num2 But I can’t add another part of the variable to the for loop, for (auto& i, i2 : arr, arr2)

Are there other ways to do this?

#include <charconv>

int64_t a = 123567893,
        b = 85162,
        test = 0,
        test2 = 0;

    string arr = to_string(a),
           arr2 = to_string(b),
           num,
           num2 ;

    for (auto& i : arr, arr2)
    {
        num.push_back(i);
        num2.push_back(i);
        cout << i;
        // cout << i2;
    }

    from_chars(num.data(), num.data() + num.size(), test);
    from_chars(num2.data(), num2.data() + num2.size(), test2);


    cout << "\n" << test << endl;
    cout << "\n" << test2 << endl;
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
loli
  • 17
  • 6

3 Answers3

2

You can use boost

#include <boost/range/combine.hpp>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    std::string arr = "ABC";
    std::string arr2 = "XYZ";
    std::string num, num2;
    assert(arr.size() == arr2.size());
    for (const auto &i : boost::combine(arr, arr2)) {
        decltype(arr)::value_type a;
        decltype(arr2)::value_type b;
        boost::tie(a, b) = i;
        num.push_back(a);
        num2.push_back(b);
        cout << a;
        cout << b;
    }
}

Output is

AXBYCZ

I don't know if there is a solution for range-based for loops without boost . Of course, you could

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int main() {
    std::string arr = "ABC";
    std::string arr2 = "XYZ";
    std::string num, num2;
    for (unsigned int i{0}; i < std::max(arr.size(), arr2.size()); ++i) {
        if (i < arr.size()) num.push_back(arr[i]);
        if (i < arr2.size()) num2.push_back(arr2[i]);
        if (i < arr.size()) cout << arr[i];
        if (i < arr2.size()) cout << arr2[i];
    }
}
Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62
  • @loli AFAIK there is no option that meets all your requirements. You could use a simple loop. You could use two loops. You could implement the zip function from boost. – Thomas Sablik Oct 18 '19 at 10:19
  • I need to somehow perform interactions between arrays, and if I make two loops, I will not be able to interact with them when I parse the array. – loli Oct 18 '19 at 10:26
  • @loli You can use a basic loop. I added a code example to my answer – Thomas Sablik Oct 18 '19 at 10:27
  • Thank you, I can somehow get around the size requirements of the array. eg: `arr = "123"; arr2 = "1234567"` array2 is truncated – loli Oct 18 '19 at 10:43
  • That depends on the use case. What should happen after the end of the shorter array? _"I need to somehow perform interactions between arrays"_ How can they interact after the end of one array? I added an idea to my answer but that makes no sense. Usually you have to increase the size of the smaller array and fill it with default values. – Thomas Sablik Oct 18 '19 at 10:48
  • Thanks, I got a little confused, but now I realized that the arrays should be the same. – loli Oct 18 '19 at 10:54
  • can I find out what means `i{ 0 }` ? – loli Oct 18 '19 at 11:08
  • this is the same as `i = 0;`? – loli Oct 18 '19 at 11:14
  • @loli Yes: https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Res-list – Thomas Sablik Oct 18 '19 at 11:47
1

This here

for (auto& i : arr, arr2)

Won't work because that's not what the comma operator does. You can read about the comma operator here. In short, arr, arr2 returns arr2 so for (auto& i : arr, arr2) is equivalent to for (auto& i : arr2).

Instead, since arr and arr2 are std::string, you can just concatenate them with +:

for (auto& i : arr + arr2)
{
    num.push_back(i);
}
Blaze
  • 16,736
  • 2
  • 25
  • 44
-1

Why not this

for_each(arr.begin(),arr.end(), [&num](char c) {num.push_back(c);})
for_each(arr2.begin(),arr2.end(), [&num](char c) {num.push_back(c);})
v78
  • 2,803
  • 21
  • 44