简体   繁体   English

如何在不使用 append 函数的情况下附加到 C++ 字符串?

[英]How do I append to C++ string without using append function?

I need to append the value of string2 to string1 without using append method in string library.我需要将 string2 的值附加到 string1 而不使用字符串库中的 append 方法。 So far, I've only managed to do this:到目前为止,我只能做到这一点:

#include<iostream>
#include<string>
using namespace std;
int main(){
    string s1="Messi ";
    string s2="best";
    int i=s1.size();
    int j=s2.size();
    int count;
    for(count=0;count<=j;count++){
        s1[i++]=s2[count];

    }   
    cout<<s1;
}

But this, only gives me the value of s1 which is "Messi ".但这,只给了我 s1 的值,即“梅西”。 However, I checked by doing this: s1[8].但是,我通过这样做进行了检查:s1[8]。 Since, s1 has index 5, s1[8] shouldn't work, right?由于 s1 的索引为 5,因此 s1[8] 不应该工作,对吗? But, it does and I get "s" which is in s2.但是,确实如此,我得到了 s2 中的“s”。 So, it means that the value of s2 is indeed going in s1 but somehow, typing s1 only shows "Messi".所以,这意味着 s2 的值确实在 s1 中,但不知何故,输入 s1 只显示“梅西”。

The reason I ask this because we were given this set of instructions to implement the program(yes it appears to be used on c strings but we were told to implement on string object):我问这个的原因是因为我们得到了这组指令来实现程序(是的,它似乎在 c 字符串上使用,但我们被告知要在字符串对象上实现):

Step 1: Initialize i= strlen(s1)
Step 2: Initialize j=strlen(s2)
Step 3: Initialize count=0
Step 4: Repeat steps 5 to 7 while count<=j
Step 5: s1[i]=s2[count]
Step 6: i=i+1
Step 7: count=count+1

I think this is what you want我想这就是你想要的

#include<iostream>
#include<string>
using namespace std;
int main(){
    string s1="string1 ";
    string s2="string2";
    int i=s1.size();
    int j=s2.size();
    s1.resize(i+j);

    for(int count=0;count<=j;count++){

        s1[count+i]=s2[count];

    }   
    cout<<s1;
}

After the modifications you've made, I think the code you are looking for is as follows经过你的修改,我想你要找的代码如下

#include<iostream>
#include<cstring>
using namespace std;
int main(){
    char s1[10]="str1 ";
    char s2[]="str2";
    int i=strlen(s1);
    int j=strlen(s2);
    int count = 0;
    for(;count<=j;){
        s1[i]=s2[count];
        i=i+1;
        count=count+1;

    }   
    cout<<s1;
}

std::string it's not meant to be used that way. std::string不应该这样使用。 Class allocates its storage sand operator[] cannot expand it, so string cannot become longer.类分配它的存储沙子操作符[]不能扩展它,所以字符串不能变长。 Documented and guaranteed way to append s2 to s1 is将 s2 附加到 s1 的记录和保证方法是

s1 += s2;

Then you shouldn't use std::string because doing that with it is undefined behavior.. Actually, provided length of those initial values, std::string allowed not to use dynamic memory and use class's storage and you have no way to know if it's so from user level of access.那么你不应该使用std::string因为这样做是未定义的行为..实际上,提供这些初始值的长度, std::string允许不使用动态内存并使用类的存储,你无法知道如果从用户访问级别来看是这样。 It's implementation details.这是实现细节。

You have to resize() string before writing in it and hold on idea that size() of string as an index unlike length includes zero character.您必须在写入之前resize()字符串,并坚持将字符串的size()作为索引而不是长度包含零字符的想法。 Resize can change type of storage and allocate storage needed to hold second string.调整大小可以更改存储类型并分配保存第二个字符串所需的存储空间。 max_size() returns actual amount characters string can store without reallocation. max_size()返回字符串可以在不重新分配的情况下存储的实际数量。

You can either use += operator or just use insert :您可以使用+=运算符或仅使用insert

string s1 = "Messi ";
string s2 = "best";

s1.insert(s1.size(), s2);
cout << s1 << endl;
cout << s1[8] << endl; // s

Or with iterators:或者使用迭代器:

s1.insert(s1.cend(), s2.cbegin(), s2.cend());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM