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.
  • Dummie1138D Offline
    Dummie1138D Offline
    Dummie1138
    wrote on 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.

    JonBJ 1 Reply Last reply
    0
    • Dummie1138D Dummie1138

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on 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".

      Dummie1138D 1 Reply Last reply
      0
      • JonBJ JonB

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

        Dummie1138D Offline
        Dummie1138D Offline
        Dummie1138
        wrote on last edited by
        #3

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

        U JonBJ 2 Replies Last reply
        0
        • Dummie1138D Dummie1138

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

          U Offline
          U Offline
          uod_
          wrote on last edited by uod_
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • Dummie1138D Dummie1138

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

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on 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
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on 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));
              
              JonBJ 1 Reply Last reply
              2
              • Chris KawaC Chris Kawa

                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));
                
                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

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

                Chris KawaC 1 Reply Last reply
                0
                • JonBJ JonB

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

                  Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on 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.

                  JonBJ 1 Reply Last reply
                  0
                  • Chris KawaC Chris Kawa

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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.

                    Chris KawaC 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @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 KawaC Offline
                      Chris KawaC Offline
                      Chris Kawa
                      Lifetime Qt Champion
                      wrote on 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...

                      JonBJ 1 Reply Last reply
                      1
                      • Chris KawaC Chris Kawa

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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on 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

                        • Login

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