Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Appending a QString with QVector
QtWS25 Last Chance

Appending a QString with QVector

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 709 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dummie1138
    wrote on 20 Oct 2022, 13:41 last edited by
    #1

    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.

    J 1 Reply Last reply 20 Oct 2022, 14:01
    0
    • D Dummie1138
      20 Oct 2022, 13:41

      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.

      J Offline
      J Offline
      JonB
      wrote on 20 Oct 2022, 14:01 last edited by JonB
      #2

      @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".

      D 1 Reply Last reply 20 Oct 2022, 14:44
      0
      • J JonB
        20 Oct 2022, 14:01

        @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".

        D Offline
        D Offline
        Dummie1138
        wrote on 20 Oct 2022, 14:44 last edited by
        #3

        @JonB That is true, my QVector binMantissa only contains 1s and 0s. Does that change anything?

        U J 2 Replies Last reply 20 Oct 2022, 14:49
        0
        • D Dummie1138
          20 Oct 2022, 14:44

          @JonB That is true, my QVector binMantissa only contains 1s and 0s. Does that change anything?

          U Offline
          U Offline
          uod_
          wrote on 20 Oct 2022, 14:49 last edited by uod_
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • D Dummie1138
            20 Oct 2022, 14:44

            @JonB That is true, my QVector binMantissa only contains 1s and 0s. Does that change anything?

            J Offline
            J Offline
            JonB
            wrote on 20 Oct 2022, 14:57 last edited by
            #5

            @Dummie1138 said in Appending a QString with QVector:

            Does that change anything?

            No, it just makes it legitimate for a binary representation!

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 20 Oct 2022, 15:15 last edited by Chris Kawa
              #6

              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));
              
              J 1 Reply Last reply 20 Oct 2022, 18:03
              2
              • C Chris Kawa
                20 Oct 2022, 15:15

                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));
                
                J Offline
                J Offline
                JonB
                wrote on 20 Oct 2022, 18:03 last edited by
                #7

                @Chris-Kawa
                Question: would you not just write it like the OP originally showed, and be happy? :)

                C 1 Reply Last reply 20 Oct 2022, 18:12
                0
                • J JonB
                  20 Oct 2022, 18:03

                  @Chris-Kawa
                  Question: would you not just write it like the OP originally showed, and be happy? :)

                  C Offline
                  C Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on 20 Oct 2022, 18:12 last edited by
                  #8

                  @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 of QString::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.

                  J 1 Reply Last reply 20 Oct 2022, 18:15
                  0
                  • C Chris Kawa
                    20 Oct 2022, 18:12

                    @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 of QString::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.

                    J Offline
                    J Offline
                    JonB
                    wrote on 20 Oct 2022, 18:15 last edited by JonB
                    #9

                    @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.

                    C 1 Reply Last reply 20 Oct 2022, 18:22
                    0
                    • J JonB
                      20 Oct 2022, 18:15

                      @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.

                      C Offline
                      C Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 20 Oct 2022, 18:22 last edited by Chris Kawa
                      #10

                      @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...

                      J 1 Reply Last reply 20 Oct 2022, 18:23
                      1
                      • C Chris Kawa
                        20 Oct 2022, 18:22

                        @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...

                        J Offline
                        J Offline
                        JonB
                        wrote on 20 Oct 2022, 18:23 last edited by
                        #11

                        @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.

                        1 Reply Last reply
                        0

                        1/11

                        20 Oct 2022, 13:41

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved