简体   繁体   中英

C++ string concatenation

I do Exercise 6 in Chapter 2 from book Thinking In C++, 2nd Edition, Volume 1 ©2000 by Bruce Eckel.

Original problem:

Change Fillvector.cpp so that it concatenates all the elements in the vector into a single string before printing it out, but don't try to add line numbering.

The file Fillvector.cpp is available at http://www.BruceEckel.com .

The following code is what I wrote to solve the exercise:

// Fillvector.cpp
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<string> v;
    ifstream in("Fillvector.cpp");
    string line, ss;

    while (getline(in, line)) {
        v.push_back(line);
    }

    for (vector<string>::size_type i = 0; i < v.size(); i++) {
        ss += v[i];
    }

    cout << ss;

    return 0;
}

The code compiled successfully and I got the produced result:

} return 0;s;[i];ing>::size_type i = 0; i < v.size(); i++) {

The output was not what I thought it should be. I thought the output must be as follow:

// Fillvector.cpp#include <string>#include <fstream>#include <iostream>#include <vector>using namespace std:int main() {vector<string> v;ifstream in("Fillvector.cpp");string line, ss;while(getline(in, line))v.push_back(line);for(string::size_type i = 0; i < v.size(); i++)ss += v[i];cout << ss;return 0;}

I read information about string concatenation operator at http://www.cplusplus.com/reference/string/string/operator+=/ but still do not know why the output is wrong. Please explain this output for me. Any help would be much appreciated.

You've done it correctly, but you opened the file "FillVector.cpp". To get the output you want, you have to open your source code's file. So if you named your program code "a.cpp" then you should open "a.cpp" to get your source code concatenated into a single line.

Also, on line 14 you use string::size_type to iterate over a vector. It should be vector<string>::size_type . That said, a simple size_t would do as well.

Also, don't forget to indent. (Lines 13 and 15.)

Hope this helps.

You are running on windows and your output is :-

// Fillvector.cpp\r#include <string>\r#include <fstream>\r#include <iostream>

This looks more wrong than it really is, as the \\r moves the cursor to the beginning of the line.

Your code worked properly for me (once I changed the : to the ; as I indicated in the edit. I got the result you wanted:

$ g++ FillVector.cpp
$ ./a.out
#include <string>#include <fstream>#include <iostream>#include <vector>using namespace std;int main() {    vector<string> v;    ifstream in("Fillvector.cpp");    string line, ss;    while(getline(in, line)) {        v.push_back(line);    }    for(string::size_type i = 0; i < v.size(); i++)  {        ss += v[i];    }    cout << ss;    return 0;}$ 

Here is your code with better indentation:

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

int main() {
    vector<string> v;
    ifstream in("Fillvector.cpp");
    string line, ss;

    while(getline(in, line)) {
        v.push_back(line);
    }
    for(string::size_type i = 0; i < v.size(); i++)  {
        ss += v[i];
    }

    cout << ss;

    return 0;
}

However, if you want your code to produce sensible output on Windows, you will need to force a newline between each string. Try this:

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

int main() {
    vector<string> v;
    ifstream in("Fillvector.cpp");
    string line, ss;

    while(getline(in, line)) {
        v.push_back(line);
    }
    for(string::size_type i = 0; i < v.size(); i++)  {
        ss += v[i] + std::string("\n");  // Add a \n for windows!
    }

    cout << ss;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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