C++

C++ string append

The word “append” means to add something at the back of another thing. A string can be declared in C++ in two main ways. Declaring a string as an array-of-chars or as a constant-pointer-to-chars is one way. Instantiating a string object data structure from the string class is another way. To instantiate a string object from the string class, the C++ string library has to be included in the program.

Consider the following target string:

    "Dancing on the moon"

‘!’ as a character, can be appended to the target string, for it to become,

    "Dancing on the moon!"

The substring “s surface”, can be appended to the target string, for it to become,

    "Dancing on the moon's surface"

The C++ string class has the member function, append(), for appending. There are actually 9 variants of this member function in C++20. Such variants of functions are called overloaded functions. The 9 overloaded functions will be explained in this article, beginning with the simplest. The C++ string push_back() function will also be explained. Strings declared as array-of-chars or as constant-pointer-to-chars are not considered for appending. Only strings instantiated from the string class are considered for appending.

basic_string& append(const T& t)

This member function returns the appended string. It takes as argument, an array-of-chars. The following code illustrates this:

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

    int main()
    {
        string target = string("Dancing on the moon");
        char chs[] = "'s surface";
        string result = target.append(chs);
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surface

Note: both the original and returned strings are appended.

basic_string& append(const charT* s)

This member function is very similar to the above function. It takes a constant pointer to chars as an argument and returns the appended string. The following program illustrates this:

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

    int main()
    {
        string target = string("Dancing on the moon");
        const char* cpc = "'s surface";
        string result = target.append(cpc);
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surface

basic_string& append(initializer_list<charT>)

This member function is similar to the above, but it takes the string list itself as an argument. The list can be a string literal in double quotes, or a sequence of characters, ending with the NUL (\0) character in braces. The function returns the appended string. The following program illustrates this, for the string literal in double-quotes:

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

    int main()
    {
        string target = string("Dancing on the moon");
        string result = target.append("'s surface");
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surface

The following program illustrates this for the appending string, as array-of-characters:

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

    int main()
    {
        string target = string("Dancing on the moon");
        string result = target.append({'\'','s',' ','s','u','r','f','a','c','e','\0'});
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surface

basic_string& append(const basic_string& str)

This member function is similar to the above, but it appends all the lists of another instantiated string object. The function returns the appended string. The following program illustrates this (for two string objects):

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

    int main()
    {
        string target = string("Dancing on the moon");
        string otherStr = string("'s surface");
        string::iterator it = otherStr.begin();
        string result = target.append(otherStr);
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surface

basic_string& append(InputIterator first, InputIterator last)

This member function is similar to the above, but for its arguments, it takes a range from another instantiated string object, where “first” is an iterator that points to the first element of the range, and “last” is another iterator that points just after the last element of the range. The function returns the appended string. The following program illustrates this for two string objects:

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

    int main()
    {
        string target = string("Dancing on the moon");
        string otherStr = string("The earth's surface is not smooth");
        string::iterator it = otherStr.begin();
        string::iterator fst = it + 9;
        string::iterator lst = it + 19;
        string result = target.append(fst, lst);
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surface

Notice how the fst and lst iterators were determined.

basic_string& append(const charT* s, size_type n)

This member function is similar to the above, but it appends the first n characters of the character sequence, pointed to by a constant-pointer-to-char. The function returns the appended string. The following program illustrates this:

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

    int main()
    {
        string target = string("Dancing on the moon");
        const char* cpc = "'s surface";
        string result = target.append(cpc, 7);
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surf

basic_string& append(const T& t, size_type pos, size_type n = npos)

This member function is similar to the above, but it appends n characters of the character sequence pointed to, by a constant-pointer-to-char, beginning from index, pos. The function returns the appended string. The following program illustrates this:

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

    int main()
    {
        string target = string("Dancing on the moon");
        const char* cpc = "'s surface";
        string result = target.append(cpc, 2, 5);
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon surf

If the third argument is absent, then the characters are taken from pos to the end of its string.

basic_string& append(const basic_string& str, size_type pos, size_type n = npos)

This member function is similar to the above, but the other string is an instantiated string object and not a constant-pointer-to-char. The function returns the appended string. The following program illustrates this:

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

    int main()
    {
        string target = string("Dancing on the moon");
        string otherStr = "'s surface";
        string result = target.append(otherStr, 2, 5);
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon surf

If the third argument is absent, then the characters are taken from pos to the end of its string.

basic_string& append(size_type n, charT c)

This member function can append n number of the same character, c. The following program, illustrates this:

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

    int main()
    {
        string target = string("Dancing on the moon");
        char ch = '!';
        string result = target.append(3, ch);
        cout << result << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon!!!

void push_back(charT c)

The push_back() function returns void. It appends only one character, c. The following program illustrates this:

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

    int main()
    {
        string target = string("Dancing on the moon");
        char ch = '!';
        target.push_back(ch);
        cout << target << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon!

Since no new string is returned, the original target string ought to be changed.

Inserting

The string class also has a member function, insert(). It is also an overloaded function of different variants. One of the main arguments for the insert function is an iterator. If the insert() function behaves as the append() function, then it needs an iterator that points just after the last character of the string. All insert() functions change the original target and do not return the appended string.

The following program shows how a single character is appended to a target string, using the insert() function:

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

    int main()
    {
        string target = string("Dancing on the moon");
        string::iterator it = target.end();
        target.insert(it, '!');
        cout << target << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon!

The following program shows how an initializer list is appended to a string:

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

    int main()
    {
        string target = string("Dancing on the moon");
        string::iterator it = target.end();
        target.insert(it, {'\'','s',' ','s','u','r','f','a','c','e','\0'});
        cout << target << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surface

Instead of using the iterator, the insert() function can use the number, which is one greater than the maximum index. This number can be gotten by the expression, stringObj.size(). The following code illustrates this:

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

    int main()
    {
        string target = string("Dancing on the moon");
        string::iterator it = target.end();
        const char* cpc = "'s surface";
        target.insert(target.size(), cpc);
        cout << target << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surface

The following program is similar to the above, but the other string, is a string object:

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

    int main()
    {
        string target = string("Dancing on the moon");
        string::iterator it = target.end();
        string otherStr =  string("'s surface");
        target.insert(target.size(), otherStr);
        cout << target << endl;
   
        return 0;
    }

The output is:

    Dancing on the moon's surface

Conclusion

To append to a target string, use the string class append() member function. There are nine overloaded variants of the append() member function in C++. To append just one character, the push_back() member function can be used. The insert() member function can also be used to append. In this case, the position, just after the last character of the string, has to be accessed.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.