What data type should I use as a buffer?

Igor

I'm writing a simple chat server and I'm wondering what data type should I use for a buffer. I was thinking about string (which would be quite comfortable in my case), however many times I saw people writing that string should not be used as a buffer (better to use vector<char>) but is it always the case?

In my program I would like to read some message from the client into a buffer, send that data to some other client and also store that message in database. I use SQLite so, with string (skipping db initialization) it would look like this:

std::string buffer;
buffer.resize(1024);

// read some data into the buffer
socket.async_read_some(boost::asio::buffer(&buffer[0], 1024),
    [this, self](boost::system::error_code ec, size_t length)
{
    buffer.resize(length);

    // do some other things with buffer - send to another user ...

    std::string query = "INSERT INTO messages (message) VALUES (\"" + buffer "\")";

    sqlite3_exec(database, query.c_str(), sql_callback, NULL, &err_msg);
}

If I was to use vector<char> instead, I would firstly need to convert it to a string (or maybe there is another way?)

So what should I use?

WhiZTiM

If your use-case mostly requires some "conversion" to std::string, then it may be a good idea to simply use std::string. The most obvious difference between using std::string as a character buffer vs std::vector<char> is that the former is permitted to do some magic Short String Optimization (depends on library vendor). While the latter isn't permitted to do so. Your Mileage May Vary.

If I was to use vector<char> instead, I would firstly need to convert it to a string (or maybe there is another way?)

Yes you would have to do a std::vector<char> to std::string "conversion" like this:

std::string query = "INSERT INTO messages (message) VALUES (\"" + std::string(buffer.begin(), buffer.end()) + "\")";

With some work, you can avoid creating temporaries (and multiple memory allocations) resulting from both buffer "conversion" and concatenation using std::string::operator +.

std::string build_query(const char* left_string, std::size_t left_string_size,
                        const std::vector<char>& buffer,
                        const char* right_string, std::size_t right_string_size)
{
    std::string query;
    query.reserve(left_string_size + buffer.size() + right_string_size + 1);
    query.append(left_string);
    query.append(buffer.begin(), buffer.end());
    query.append(right_string);
    return query;
}

And use like:

std::vector<char> buffer(24);

constexpr char left[] = "INSERT INTO messages (message) VALUES (\"";
constexpr char right[] = "\")";
std::string query = build_query( left, len(left), buffer, right, len(right) );

See a full example here: Live On Coliru

On the long run, you could go further and write some neat template helpers to cover more general cases of building up query string from std::vector<char>.

As for performance, you will have to measure. Though, while not a valid measure of performance, GCC and Clang actually emits fewer assembly instructions for build_query than the regular "conversion".

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

TOP 一覧

  1. 1

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  2. 2

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  3. 3

    CSSのみを使用して三角形のアニメーションを作成する方法

  4. 4

    ドロップダウンリストで選択したアイテムのQComboBoxスタイル

  5. 5

    ZScalerと証明書の問題により、Dockerを使用できません

  6. 6

    PyCharmリモートインタープリターはプロジェクトタブにサイトパッケージのコンテンツを表示しません

  7. 7

    Windows 10でのUSB入力デバイスの挿入/取り外しの検出

  8. 8

    Excel - count multiple words per cell in a range of cells

  9. 9

    PictureBoxで画像のブレンドを無効にする

  10. 10

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  11. 11

    スタート画面にシャットダウンタイルを追加するにはどうすればよいですか?

  12. 12

    Python / SciPyのピーク検出アルゴリズム

  13. 13

    Luaの文字列から特定の特殊文字を削除するにはどうすればよいですか?

  14. 14

    Pythonを使用して、リストからデータを読み取り、特定の値をElasticsearchにインデックス付けするにはどうすればよいですか?

  15. 15

    LinuxでPySide2(Qt for Python)をインストールするQt Designerはどこにありますか?

  16. 16

    goormIDEは、ターミナルがロードするデフォルトプロジェクトを変更します

  17. 17

    QGISとPostGIS(マップポイント(米国の地図上にraduisを使用した緯度と経度)

  18. 18

    MLでのデータ前処理の背後にある直感

  19. 19

    ターミナルから「入力ソースの変更」ショートカットを設定する

  20. 20

    パンダは異なる名前の列に追加します

  21. 21

    同じクラスの異なるバージョンを使用したクラスローディング:java.lang.LinkageError:名前の重複クラス定義を試行しました

ホットタグ

アーカイブ