將整數插入字符串 C++ 的中間

米格爾

我試圖用數字 5 替換 W(在本例中)。但是,當我嘗試時,我只得到 5 的 ascii 值來替換 W,而不是數字 5 本身。我將如何解決這個問題?
注意:這是一個較長項目的縮短示例。我需要訪問 nums[1] 中的數字。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int nums[3] {4,5,6};
    string str = "HELLO WORLD";

    cout << str << endl;
    str[6] = nums[1];
    cout << str << endl;

    return 0;
}

輸出:你好
世界
你好♣ORLD

謝爾蓋·科列斯尼克

您可以將數字轉換為 ASCII 字符:(char c = '0' + 5給您'5')。
這是您修復的代碼:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int nums[3] {4,5,6};
    string str = "HELLO WORLD";

    cout << str << endl;
    str[6] = '0' + nums[1];
    cout << str << endl;

    return 0;
}

但是,如果您打算像標題所說的那樣插入整數,則必須執行以下操作:

#include <iostream>
#include <string>

using namespace std;

int main()
{
    int nums[3] {4,15,6};
    string str = "HELLO WORLD";

    cout << str << endl;
    // str = str.substr(0, 6) + std::to_string(nums[1]) + str.substr(7);
    str.replace(6, 1, std::to_string(nums[1])); // as Remy Lebeau has mentioned in the comments
    cout << str << endl;

    return 0;
}

(UPD:它相當於std::string::replace)這是一個更複雜的版本,它嘗試使用現有容量:


#include <iostream>
#include <string>
#include <algorithm>

// don't ever do it with std
// using namespace std;

size_t count_digits(int num)
{
    int digits = 0; 
    do { 
        num /= 10; 
        digits++; 
    } while (num);
    return digits;
}

void insert_number(std::string& str, size_t indx, int num)
{
    const size_t digits = count_digits(num),
        oldSize = str.size();
    
    // insert will reallocate only if capacity is not enough
    str.insert(str.size(), digits - 1, '0');

    // move the end of string right by rotating (rewriting characters)
    auto iterBegin = std::next(str.begin(), indx),
        iterNext = std::next(str.begin(), oldSize);
    std::rotate(iterBegin, iterNext, str.end());

    // write our string from integer to the designated space
    const auto &strInteger = std::to_string(num);
    for (size_t iStr = indx, iInt = 0; iInt != strInteger.size(); ++iInt, ++iStr)
        str[iStr] = strInteger[iInt];
}

int main()
{
    int nums[3] {4, 48152342, 6};
    std::string str = "HELLO WORLD LOOOOONG STRIIING";
    str.reserve(str.capacity() + 20); // strings don't preallocate more space after construction

    std::cout << str << std::endl;
    std::cout << "capacity before: " << str.capacity() << std::endl;
    insert_number(str, 6, nums[1]);
    std::cout << str << std::endl;
    std::cout << "capacity after: " << str.capacity() << std::endl;

    return 0;
}

https://godbolt.org/z/7aPMvcosj

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

將包含在字符串中的計算轉換為整數 - C++

如何將整數轉換為字符串,保留 C++ 中的前導零?(使用 to_string)

將輸入字符串傳遞給 C 中的函數

使用 c# 將字符串的輸入驗證為整數值?

在ansible中將字符串轉換為整數

將字符串連接到 Get-WmiObject 表中的整數 - PowerShell

如何在 RUST 中將字符串轉換為整數

我將如何在 C 中創建數字字符串的二維數組?

將整個字符串轉換為整數並在文本字段中快速顯示

如何在 c# 中將字符串解析為日期時間?

將有效字符串複製到 C 中的二維數組

C# 將字符串作為幾何體保存到數據庫中

在C ++中的字符串中插入字符

C ++在数组或字符串中插入字符

在字符串中間插入字符

將字符串插入到 R 中的 URL 中間

如何從字符串中提取所有整數並將它們存儲在java中的int數組中

快速在C#中的前面插入字符串

在C中插入动态字符串数组

在C中的指针字符串中间插入整数

將字符串中的字符與 C 中的另一個字符進行比較

如何在 C++ 中將大字符串數字 (uint256) 轉換為 vector<unsigned char>

如何將字符收集到 C 中的字符串中?

在 Python 中將字符串列表轉換為類別整數

在 Kotlin 中將整數列表轉換為字符串列表

如何使用 C# 將字符附加到 MongoDB 文檔中的字符串

在 C++ 中將多個字符附加到字符串

c#將反轉的字符串存儲到另一個字符串中

C# 在表示字符串的大字節數組中查找子數組