Appending a QString with QVector
-
Hi. I have a QString binResult meant to represent a binary value. I wish to append this QString with a QVector<int>, binMantissa. I am using a QVector since I'm on Qt5. I have the following code so far.
for (int i = 0; i <= binMantissa.size(); i++){ binResult.append(QString::number(binMantissa.at(i))); }
Is there a function that would achieve what I wish to achieve without using a for-loop? I was unable to find anything on QString to QVectors on the documentation.
-
Hi. I have a QString binResult meant to represent a binary value. I wish to append this QString with a QVector<int>, binMantissa. I am using a QVector since I'm on Qt5. I have the following code so far.
for (int i = 0; i <= binMantissa.size(); i++){ binResult.append(QString::number(binMantissa.at(i))); }
Is there a function that would achieve what I wish to achieve without using a for-loop? I was unable to find anything on QString to QVectors on the documentation.
@Dummie1138 said in Appending a QString with QVector:
Is there a function that would achieve what I wish to achieve without using a for-loop?
No.
And btw I assume your
binMantissa
only contains values of 0 or 1, since you say "QString binResult meant to represent a binary value". -
@Dummie1138 said in Appending a QString with QVector:
Is there a function that would achieve what I wish to achieve without using a for-loop?
No.
And btw I assume your
binMantissa
only contains values of 0 or 1, since you say "QString binResult meant to represent a binary value".@JonB That is true, my QVector binMantissa only contains 1s and 0s. Does that change anything?
-
@JonB That is true, my QVector binMantissa only contains 1s and 0s. Does that change anything?
-
@JonB That is true, my QVector binMantissa only contains 1s and 0s. Does that change anything?
@Dummie1138 said in Appending a QString with QVector:
Does that change anything?
No, it just makes it legitimate for a binary representation!
-
In C++20:
std::ranges::transform(binMantissa, std::back_inserter(binResult), [](int i) { return QChar('0' + i); });
or in C++11:
std::transform(binMantissa.cbegin(), binMantissa.cend(), std::back_inserter(binResult), [](int i) { return QChar('0' + i); });
Or, if your numbers are more than one digit long you can make a little adapter:
struct QStringAppender { using difference_type = ptrdiff_t; QString* s; QStringAppender(QString& s) : s(&s) {} QStringAppender& operator*() noexcept { return *this; } QStringAppender& operator++() noexcept { return *this; } QStringAppender& operator++(int) noexcept { return *this; } QStringAppender& operator=(const QString& val) { s->append(val); return *this; } };
and then you can do
std::ranges::transform(binMantissa, QStringAppender(binResult), std::bind(qOverload<int, int>(&QString::number), std::placeholders::_1, 10));
-
In C++20:
std::ranges::transform(binMantissa, std::back_inserter(binResult), [](int i) { return QChar('0' + i); });
or in C++11:
std::transform(binMantissa.cbegin(), binMantissa.cend(), std::back_inserter(binResult), [](int i) { return QChar('0' + i); });
Or, if your numbers are more than one digit long you can make a little adapter:
struct QStringAppender { using difference_type = ptrdiff_t; QString* s; QStringAppender(QString& s) : s(&s) {} QStringAppender& operator*() noexcept { return *this; } QStringAppender& operator++() noexcept { return *this; } QStringAppender& operator++(int) noexcept { return *this; } QStringAppender& operator=(const QString& val) { s->append(val); return *this; } };
and then you can do
std::ranges::transform(binMantissa, QStringAppender(binResult), std::bind(qOverload<int, int>(&QString::number), std::placeholders::_1, 10));
@Chris-Kawa
Question: would you not just write it like the OP originally showed, and be happy? :) -
@Chris-Kawa
Question: would you not just write it like the OP originally showed, and be happy? :)@JonB I just answered OP's question :P ... and I'm never happy :)
I would be somewhat less unhappy about it if both standard library and Qt didn't try to be helpful ;)
std::back_inserter
tries to be helpful by not letting you specify your own value type. Qt tries to be helpful and gives you a bunch ofQString::number
overloads with bunch of default parameters. Standard tries to be helpful and puts new stuff in even more namespaces.If they all didn't do that and just let the programmer be the programmer, you could just write
std::transform(binMantissa, std::back_inserter<QString>(binResult), &QString::fromInt);
and that would be pretty, but no, we can't have nice things.
-
@JonB I just answered OP's question :P ... and I'm never happy :)
I would be somewhat less unhappy about it if both standard library and Qt didn't try to be helpful ;)
std::back_inserter
tries to be helpful by not letting you specify your own value type. Qt tries to be helpful and gives you a bunch ofQString::number
overloads with bunch of default parameters. Standard tries to be helpful and puts new stuff in even more namespaces.If they all didn't do that and just let the programmer be the programmer, you could just write
std::transform(binMantissa, std::back_inserter<QString>(binResult), &QString::fromInt);
and that would be pretty, but no, we can't have nice things.
@Chris-Kawa
It's just, I can glance at the original and see what it does/it's OK.std::ranges::transform(binMantissa, std::back_inserter(binResult), [](int i) { return QChar('0' + i); }); std::transform(binMantissa.cbegin(), binMantissa.cend(), std::back_inserter(binResult), [](int i) { return QChar('0' + i); });
I have to look hard at these. Sorry, but C++ stuff is not very pretty.
-
@Chris-Kawa
It's just, I can glance at the original and see what it does/it's OK.std::ranges::transform(binMantissa, std::back_inserter(binResult), [](int i) { return QChar('0' + i); }); std::transform(binMantissa.cbegin(), binMantissa.cend(), std::back_inserter(binResult), [](int i) { return QChar('0' + i); });
I have to look hard at these. Sorry, but C++ stuff is not very pretty.
@JonB It just takes a little time to get used to all the ::{}(){} but it's pretty simple:
std::ranges::transform(binMantissa, std::back_inserter(binResult), [](int i) { return QChar('0' + i); }); ^ ^ ^ ^ for source destination operation
almost like C's memcpy.
Think of it like looking at the code in Matrix. After a while you don't even see it. It's all just blondes, brunettes... -
@JonB It just takes a little time to get used to all the ::{}(){} but it's pretty simple:
std::ranges::transform(binMantissa, std::back_inserter(binResult), [](int i) { return QChar('0' + i); }); ^ ^ ^ ^ for source destination operation
almost like C's memcpy.
Think of it like looking at the code in Matrix. After a while you don't even see it. It's all just blondes, brunettes...@Chris-Kawa
Oh, I know how it works, and why it works, and why it's efficient to write it that way. Just I have to examine it. It's not pretty/intuitive. That's all.