C++ 动态分配字符串数组,然后更改它';s值多少?

C++ 动态分配字符串数组,然后更改它';s值多少?,c++,arrays,string,dynamic-allocation,C++,Arrays,String,Dynamic Allocation,我正在尝试创建一个字符串数组并使用指针来修改它。我不确定如何声明指针,因为字符串的长度可能不同,我认为这就是导致错误的原因 我的代码如下所示: #includes <string> #includes <iostream> using namespace std; string *users = NULL; int seatNum = NULL; cin >> seatNum; users = new string[seatNum]; string name

我正在尝试创建一个字符串数组并使用指针来修改它。我不确定如何声明指针,因为字符串的长度可能不同,我认为这就是导致错误的原因

我的代码如下所示:

#includes <string>
#includes <iostream>
using namespace std;

string *users = NULL;
int seatNum = NULL;
cin >> seatNum;
users = new string[seatNum];
string name;
cin >> name;
users[seatNum] = name;
#包括
#包括
使用名称空间std;
字符串*users=NULL;
int seatNum=NULL;
西特南;
用户=新字符串[seatNum];
字符串名;
cin>>名称;
用户[seatNum]=名称;
当我试图更改它的值时,它会引发写入访问冲突。从我读到的内容来看,这是因为字符串被编译为只读,所以我的问题是我将如何/我将如何更改它?最好是易于理解的解释。

以下几点:

  • int seatNum将在堆栈上分配,并且永远不会为NULL。您应该将其设置为0

  • 您正在设置用户[seatNum],这超出了限制,导致程序崩溃。你 只能使用从0到seatNum-1的索引

  • 更新:克里斯是正确的。我研究了它,字符串在C++中确实是可变的。

    一些事情:

  • int seatNum将在堆栈上分配,并且永远不会为NULL。您应该将其设置为0

  • 您正在设置用户[seatNum],这超出了限制,导致程序崩溃。你 只能使用从0到seatNum-1的索引


  • 更新:克里斯是正确的。我查看了它,字符串在C++中确实是可变的。

    < p>您访问的内存超出了分配数组

    的范围。
    users = new string[seatNum];
    users[seatNum] = name;
    

    第一个元素是[0]。最后一个是[seatNum-1]

    您正在访问超出分配阵列范围的内存

    users = new string[seatNum];
    users[seatNum] = name;
    

    第一个元素是[0]。最后一个是[seatNum-1]

    您已经创建了一个
    seatNum
    元素数组。数组元素索引从
    0开始
    ,因此有效索引的范围是
    [0,seatNum-1]
    。通过访问
    users[seatNum]=…
    您实际上已经超过了数组的最后一个有效元素。这将调用UB(未定义的行为)


    我看到您已经做出了正确的选择,使用
    std::string
    而不是C样式的字符串。为什么不在阵列上做同样的选择呢

    #include <string>
    #include <array> 
    #include <iostream>
    
    int main(int, char*[]) {
        int seatNum = 0;
        std::cin >> seatNum;
        std::vector<std::string> users(seatNum);
        std::cin >> users[0];
        return 0;
    }
    
    #包括
    #包括
    #包括
    int main(int,char*[]){
    int seatNum=0;
    西特南;
    std::病媒用户(seatNum);
    std::cin>>用户[0];
    返回0;
    }
    

    尽量避免使用指针和C样式的数组,尤其是动态数组。

    您已经创建了一个包含
    seatNum
    元素的数组。数组元素索引从
    0开始
    ,因此有效索引的范围是
    [0,seatNum-1]
    。通过访问
    users[seatNum]=…
    您实际上已经超过了数组的最后一个有效元素。这将调用UB(未定义的行为)


    我看到您已经做出了正确的选择,使用
    std::string
    而不是C样式的字符串。为什么不在阵列上做同样的选择呢

    #include <string>
    #include <array> 
    #include <iostream>
    
    int main(int, char*[]) {
        int seatNum = 0;
        std::cin >> seatNum;
        std::vector<std::string> users(seatNum);
        std::cin >> users[0];
        return 0;
    }
    
    #包括
    #包括
    #包括
    int main(int,char*[]){
    int seatNum=0;
    西特南;
    std::病媒用户(seatNum);
    std::cin>>用户[0];
    返回0;
    }
    

    尽量避免使用指针和C样式数组,尤其是动态数组。

    首先,不能将null值设置为int类型数据

    int seatNum=NULL;//错
    int seatNum=0;//正确的
    
    第二个字符串范围是从0到seatnum-1

    users[seatNum-1]=name;//正确的
    
    首先,不能将空值设置为int类型数据

    int seatNum=NULL;//错
    int seatNum=0;//正确的
    
    第二个字符串范围是从0到seatnum-1

    users[seatNum-1]=name;//正确的
    
    users=新字符串[seatNum]
    创建一个从
    0
    seatNum-1
    索引的字符串数组,因此
    用户[seatNum]
    访问数组的边界。
    用户=新字符串[seatNum]
    创建一个从
    0
    seatNum-1
    索引的字符串数组,因此
    用户[seatNum]
    访问该数组的边界。最后一点并不正确。字符串在C++中是可变的,因此没有理由创建副本。它只是修改有问题的对象。这是否意味着对包含的字符进行深度复制各不相同。最后一点并不完全正确。字符串在C++中是可变的,因此没有理由创建副本。它只是修改有问题的对象。这是否意味着要对包含的字符进行深度复制各不相同。啊,我不知道我怎么能忽略像这样简单的事情。有时候我想你应该问问别人,谢谢!Doh,我不知道我怎么能忽略这么简单的事情。有时候我想你应该问问别人,谢谢!