簡體   English   中英

將 std::string 中的子字符串分配給 c 樣式字符串

[英]Assiging sub string from std::string to a c-style string

我知道有多種方法可以從 std::string 轉換為 c 樣式,但我遇到的問題是這個錯誤:4 IntelliSense: expression must be a modifiable lvalue 誰能告訴我問題是什么? 此外,您能否澄清如何有效地轉換為 c 樣式字符串並在這種特殊情況下對其進行分配?

謝謝

#include <iostream>
#include <string>
#include <algorithm>
#include <stdlib.h>

using namespace std;

class Addition
{
private:
    int num1[255], num2[255], sum[255];
    char str1[255], str2[255];
    string str;
    int len1, len2;
public:
    Addition(){};
    void m_add();
};

void Addition::m_add()
{
    scanf("%s", str);
    int pos = find(str[0], str[255], ' ');
    &str1 = str.substr(0, pos);
    &str2 = str.substr(++pos);
    //scanf("%s", &str1);
    //scanf("%s", &str2);
    /*convert from a character to an int*/
    for (len1 = 0; str1[len1] != '\0'; len1++)
    {
        num1[len1] = str1[len1] - '0';
    }
    for (len2 = 0; str2[len2] != '\0'; len2++)
    {
        num2[len2] = str2[len2] - '0';
    }
    if (str1 <= 0)
    {
        cout << "Invalid input\n";
    }
    int carry = 0;
    int k = 0; //keeps track of index loop stopped at
    //start adding from the end of the array
    int idx1 = len1 - 1;
    int idx2 = len2 - 1;
    //adds only for the size of teh smallest array
    for (; idx1 >= 0 && idx2 >= 0; idx1--, idx2--, k++)
    {
        //we will have to read values stored in sum in reversed order
        sum[k] = (num1[idx1] + num2[idx2] + carry) % 10;
        //using truncation to our benefit
        //carry over can only ever be one thats why we use /10 not %10
        carry = (num1[idx1] + num2[idx2] + carry) / 10;
    }
    /*takes care of the digits not added to sum from bigger array*/
    //if the first array is bigger...
    if (len1 > len2)
    {
        while (idx1 >= 0){
            sum[k++] = (num1[idx1] + carry) % 10;
            carry = (num1[idx1--] + carry) / 10;
        }
    }
    //if the second array is bigger
    else if (len1 < len2)
    {
        while (idx2 >= 0){
            sum[k++] = (num2[idx2] + carry) % 10;
            carry = (num2[idx2--] + carry) / 10;
        }
    }
    //that you have a carry ove to the very end of the number
    if (carry != 0){
        sum[k++] = carry;
    }

    cout << "sum = ";
    //print out digits in 'sum' array from back to front
    for (k--; k >= 0; k--)
    {
        cout << sum[k];
    }
    cout << '\n';
}
int main(){

    Addition inst1;
    int n;
    cin >> n;

    for (int i = 0; i <= n; i++){
        inst1.m_add();
    }
    system("pause");
}  

我真的懷疑這是你真正想做的事情,但既然你問了......

你需要做一個strcpy

strcpy(str1, str.substr(0, pos).c_str());

查看 strcpy 的手冊頁/cppreference 了解參數是什么。

使用strncpy可能更安全,但該功能實際上並不是為了做人們使用它的目的。 不幸的是,我不相信 C++ 或 C 中實際上有一個標准函數可以執行受長度限制的 C 樣式字符串副本......沒有安全的strcpy strncpy函數在緊要關頭完成,但也要查看有關該文檔的文檔,因為它實際上用於與以空字符結尾的 c 樣式字符串完全不同的字符串格式。

substr返回值上使用c_str()可能很誘人,但如果您調用的函數試圖存儲指向該空間的指針,這可能很危險。 調用完成后, substr的返回值將被銷毀。 這將破壞c_str返回。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM