0

I want to insert the content of array of integers : int arr[n] to the vector of QStrings. std::vector<QString> vQString.- I can do it by inserting the array`s elements one by one :

vQString.push_back(QString::number(arr[i]));

By I prefer to do that using one insert operation - any advices? Thanks

YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • I'm not that familiar with Qt but according to the docs for [QString](http://qt-project.org/doc/qt-4.8/qstring.html) it doesn't support such operations. – Captain Obvlious Apr 26 '13 at 17:07
  • 3
    According to the official documentation, you can't do this in one line. you can add values one by one. write a function and call it in one line. – Barış Akkurt Apr 26 '13 at 17:12
  • 2
    Any reason you're using std::vector instead of QVector? It's certainly not wrong... but normally you adopt Qt-style containers like QVector unless you're working with external interfaces or libs that make that too annoying. Here's a good link to compare them: http://blog.codeimproved.net/2009/12/qtl-or-stl/ – darron Apr 26 '13 at 18:14
  • Just to clarify, are you appending to existing vector or want to copy it over to a new vector? – Vite Falcon Apr 26 '13 at 22:07

2 Answers2

0

This isn't a 1-line solution. But is an extendable solution. What you basically do is create a template function to do the conversion for you in an exception-safe manner, like below:

namespace yournamespace {
    template <typename U>
    struct NumberToString {
        QString operator()(const U& val) {
            return QString::number(val);
        }
    };

    template <typename T, typename U, typename Convertor>
    void CopyArrayToVector(QVector<T>& dst, const U* src, const size_t size) {
        QVector<T> temp;
        temp.reserve(size);
        for (int i = 0; i < size; ++i) {
            temp.push_back(convert(src[i]));
        }
        dst.swap(temp);
    }
}

Usage:

using yournamespace;
const size_t n = 10;
int *arr = new int[10];
QVector<String> dst;
CopyArrayToVector<QString,int,NumberToString<int> >(dst, arr, n);

DISCLAIMER: I'm not familiar with Qt framework. I whipped this up by looking at their documentation. Please feel free to correct me for any errors.

Vite Falcon
  • 6,575
  • 3
  • 30
  • 48
  • It appears from the usage of push_back() that they want append, not replace... although they're calling it insert. – darron Apr 26 '13 at 18:02
  • @darron: Sorry, I didn't understand what you meant in your comment :S. Are you saying I should have it as `temp.insert()` instead of `temp.push_back()`? – Vite Falcon Apr 26 '13 at 18:37
  • The "dst.swap(temp)" line is removing whatever entries were in dst to start with. Using the += overload probably matches the intention better. – darron Apr 26 '13 at 21:52
  • @darron: The function is called 'CopyTo' not 'AppendTo' :).. Of course, if that's his use-case, he can definitely create another function to do so. – Vite Falcon Apr 26 '13 at 21:54
  • True... but that's your function name, not his. Anyway, I just meant to point out that 'CopyTo' doesn't appear to be what he's asking... even if it is trivial to convert it. Not a big deal. – darron Apr 26 '13 at 21:57
0
const int n = 5;
int arr[n] = { 4, 6, 2, 3, 1 };
vector< QString > v( n );

transform( arr, arr + n, v.begin(),
           [] ( int i ) { return QString::number( i ); } );

for ( const QString& str : v ) {
    cout << qPrintable( str ) << endl;
}

Slightly cheating...! Just use a for loop like everyone else.

cmannett85
  • 21,725
  • 8
  • 76
  • 119