Home  >  Q&A  >  body text

将只含有大小写和空格的字符串S拆分,用C++ string实现?

对于一个只含有大小写和空格的字符串S,按空格将其拆分为若干字符串,存放在字符串数组t中。比如“Hello World”,将其拆分为“Hello”和“World”。
我用C++实现,却总是在t[num].assign(s,i-j,j);这一句报错。请问这是为什么?应该怎么实现?
以下是这段代码。

        string s; 
        int n;//s的长度
        string *t=new string;
        string s1;
        int num=0;
        int i=0;
        while(i<n){
            cout<<"i="<<i<<endl;
            int j=0;//dancichangdu
            while(s.at(i)!=32){
                i++;
                j++;
                if(i>=n)
                    break;
            }
            t[num].assign(s,i-j,j);//报错的地方。运行到此处显示.exe"已停止工作"。
            num++;//geshu
            i++;
        }

基础不扎实,希望大神多多指点。

怪我咯怪我咯2765 days ago574

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-17 13:41:42

    I can’t remember clearly. Does string support operate[]? The string you declared is not an array

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:41:42

    I can’t help but laugh. You really asked the right question. I haven’t tested it myself, but I guess the result of running this program on Linux must be segment fault

    Look carefully at your sentence:

    t[num].assign(s, i-j, j);

    What is t? The type of t is pointer to string. Pointer types exist in the C language. C++ needs to be backward compatible, so the semantics are the same. In C language, t[num] is the syntactic sugar of *(t + num), which is completely equivalent, so the result of writing it as num[t] is exactly the same, but few people use it this way.

    Earlier you used new to apply for one unit of memory, which is the location pointed to by t. Now if you visit num locations after t, of course the access is out of bounds. The operating system terminates your program immediately for safety reasons.

    Regarding pointers, if you are a beginner in C++, it is recommended to avoid them. Pointers are inherited by C++ from the C language and are indispensable to the C language. However, their use is generally not recommended in C++ because they can easily cause various security vulnerabilities. Take your program as an example. In addition to out-of-bounds access, you also forgot to use delete to release the memory you allocated.

    Also, why @GAO was accepted as the answer, please criticize it by the way.

    • First, if string does not support operator[], how can this code be compiled?

    • Second, I can’t even confirm the common sense that string supports [ ]. Have you really used C++ string...

    reply
    0
  • 怪我咯

    怪我咯2017-04-17 13:41:42

    You can use the insert(n:int,string:s) interface of string (insert string s at position n+1, if it is an empty string, insert it at position 0).

            string s1;
            int num=0;
            int i=0;    
            while(1){
                int j=0;//dancichangdu
                while(s.at(i)!=32){
                    i++;
                    j++;
                    if(i>=n)
                        break;
                }
                string r;
                r.assign(s,i-j,j);//将一个单词放在r中
                s1.insert(0,r);//要善用stl的接口
                num++;//单词个数
                if(i<n)
                    s1.insert(0," ");
                else
                    break;
                i++;
            }
    
    
    

    The code in the question should be a memory problem, but I don’t know the details yet.

    reply
    0
  • Cancelreply