2016-11-23 96 views
2

我有一些代碼將各種元素組合到一個緩衝區中。我的代碼看起來是這樣的:是否有更簡潔的方式將std :: string合併到std :: vector <char>?

static void CreatePacket(const std::string& source, const std::string id, const std::string payload, std::vector<char>& buffer) 
{ 
    buffer.resize(source.size() + id.size() + payload.size()); 
    std::vector<char>::iterator bufferDest = buffer.begin(); 

    // Start the message 
    char MessageStart = '$'; 
    *bufferDest = MessageStart; 
    ++bufferDest; 

    // Copy the message source 
    std::copy(source.begin(), source.end(), bufferDest); 
    bufferDest += source.size(); 

    // Copy the message id 
    std::copy(id.begin(), id.end(), bufferDest); 
    bufferDest += id.size(); 
} 

調用該方法如下:

std::vector<char> buffer; 

std::string source = "AB"; 
std::string id = "CDE"; 
std::string payload = "payload"; 

CreatePacket(source, id, payload, buffer); 

我還是有點綠做事的方式std,但我感覺實現有點笨重(具體而言,必須在每個副本之後明確增加bufferDest)。有沒有更乾淨的方法來做到這一點?

如果這有所幫助,我的編譯器不支持C++ 11。

+0

你不需要明確遞增'bufferDest','的std :: copy'返回到您 –

回答

3

你可以只用一個適當的vector::insert() overload到的內容追加string在的vector結束(不需要在其他的答案顯示使用std::copystd::back_inserter代碼複雜化),例如:

buffer.insert(buffer.end(), source.begin(), source.end()); 

所以,你的功能應該是這樣的:

void CreatePacket(const std::string& source, 
        const std::string& id, 
        const std::string& payload, 
        std::vector<char>& buffer) 
{ 
    buffer.clear(); 
    buffer.reserve(source.size() + id.size() + payload.size() + 1); 

    buffer.push_back('$'); 

    buffer.insert(buffer.end(), source.begin(), source.end()); 
    buffer.insert(buffer.end(), id.begin(),  id.end() ); 
    buffer.insert(buffer.end(), payload.begin(), payload.end()); 
} 
+0

對於外行來說,這與丹的回答沒有什麼兩樣。這樣做有什麼好處嗎? –

+0

@JonCage:這與此答案不同,因爲Danh使用'std :: copy'和'std :: back_inserter',而不是我使用'std :: vector :: insert',所以這段代碼更簡單。 –

+0

@ MrC64--我不打算說他們是一樣的,只是對STL新手而言,他們看起來像彼此一樣複雜。我只是感興趣,爲什麼可以選擇其中之一。在看了兩會之後,我可以看到你們看起來更容易看到它的意圖。 –

8

我覺得這樣更清晰。

void CreatePacket(const std::string& source, const std::string& id, const std::string& payload, std::vector<char>& buffer) 
{ 
    buffer.clear(); 
    buffer.reserve(source.size() + id.size() + payload.size() + 1); 

    buffer.push_back('$'); 

    std::copy(source.begin(), source.end(), std::back_inserter(buffer)); 
    std::copy(id.begin(), id.end(), std::back_inserter(buffer)); 
    std::copy(payload.begin(), payload.end(), std::back_inserter(buffer)); 
} 
+0

是的!這正是我想到的事情:-) –

+0

@JonCage:你實際上可以進一步簡化你的代碼:'std :: copy'和'std :: back_inserter'不是必須的,除非我遺漏了一些東西。您可以使用'std :: vector :: insert',如我的其他答案中所示。 –

+0

@C64先生 - 做你的建議和這個有什麼好處?他們對我來說都是相當可行的解決方案。兩者看起來都比我的代碼更好;-) –

2

這是除了你可以從std::copy使用返回的值,從而擺脫bufferDest明確的增量幾乎是乾淨的:

static void CreatePacket(const std::string& source, const std::string id, const std::string payload, std::vector<char>& buffer) 
{ 
    buffer.resize(source.size() + id.size() + payload.size()); 
    std::vector<char>::iterator bufferDest = buffer.begin(); 

    // Start the message 
    char MessageStart = '$'; 
    *bufferDest = MessageStart; 
    ++bufferDest; 

    // Copy the message source 
    bufferDest = std::copy(source.begin(), source.end(), bufferDest); 

    // Copy the message id 
    bufferDest= std::copy(id.begin(), id.end(), bufferDest); 
} 
相關問題