3

我正在写 2 个程序,第一个程序有一个整数数组

vector<int> v = {10, 200, 3000, 40000};

然后它将向量转换为字符串

int i;
stringstream sw;
string stringword;

for (i=0;i<v.size();i++) 
{
    sw << v[i] << ',';
}
stringword = sw.str();
cout << "Vector in string : "<< stringword << endl;

然后将其写入文件

ofstream myfile;
myfile.open ("writtentext");
myfile << stringword;
myfile.close();

输出 :

Vector in string : 10,200,3000,40000

第二个程序将读取文件,将字符串转换回整数,然后将其推回向量。

编码 :

string stringword;

ifstream myfile;
myfile.open ("writtentext");
getline (myfile,stringword);
cout << "Read From File = " << stringword << endl;

cout << "Convert back to vector = " ;
for (int i=0;i<stringword.length();i++)
{
    if (stringword.find(','))
    {
        int value;
        istringstream (stringword) >> value;
        v.push_back(value);
        stringword.erase(0, stringword.find(','));
    }
}
for (int j=0;j<v.size();j++) 
{
    cout << v.at(j) << " " ;
}

问题是,它只能转换和推回第一个元素,其余的被擦除。这是输出:

Read From File = 10,200,3000,40000,
Convert back to vector = 10

我做错了什么?谢谢

4

6 回答 6

4

你的 for 循环有问题

考虑一下:

while(1) //Use a while loop, "i" isn't doing anything for you
{
    //if comman not found find return string::npos

    if (stringword.find(',')!=std::string::npos)
    {
        int value;
        istringstream (stringword) >> value;

        v.push_back(value);

       //Erase all element including comma
        stringword.erase(0, stringword.find(',')+1);
    }
    else 
       break; //Come out of loop
}

相反,只需用于std::stringstream从文件中读回

std::stringstream ss(stringword);
int value;
while (ss >> value)
{
    v.push_back(value);

    if (ss.peek() == ',')
        ss.ignore();
}

for (int j=0;j<v.size();j++)  //Fix variables
{
    cout << v.at(j) << " " ; // Can use simply v[j]
}
于 2013-08-20T13:18:03.760 回答
0
for (int j=0;j<v.size();i++) 
{
    cout << v.at(i) << " " ;
}

应该

for (int j=0;j<v.size();j++) 
{
    cout << v.at(j) << " " ;
}

i未在for循环中声明

于 2013-08-20T13:15:52.863 回答
0

您可以跳过字符串转换。所有流都可以处理int类型。

std::vector<int>输出:

#include <iostream>
#include <iterator>
#include <vector>

int main() {
    std::vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    std::ostream_iterator<int> output_iterator(std::cout, ",");
    std::copy(v.begin(), v.end(), output_iterator);
}

输入std::vector<int>

#include <iostream>
#include <vector>
#include <sstream>
using namespace std;

int main() {
    std::vector<int> v;
    int value;

    std::string line;
    while(getline(cin, line, ',')) {
        std::stringstream ss(line);
        ss >> value
        v.push_back(value);
    }
    typedef std::vector<int>::iterator iter;
    iter end = v.end();
    for(iter it = v.begin(); it != end; ++it) {
        std::cout << *it << endl;
    }
}
于 2013-08-20T13:19:15.700 回答
0

这只是你犯的一个错误:

for (int j=0;j<v.size();i++) 
{
    cout << v.at(i) << " " ;
}

但是这里的工具效率太低了:

for (int i=0;i<stringword.length();i++)
{
    if (stringword.find(','))
    {
        int value;
        istringstream (stringword) >> value;
        v.push_back(value);
        stringword.erase(0, stringword.find(','));
    }
}

你可以这样做,只是一个建议:

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>

int ConvertStringToInt(const std::string &str, std::vector<int> &ints)
{
    int count_int = 0;
    std::string string_int;

    size_t start = 0;
    size_t end   = 0;

    while ((end = str.find(',', start)) != std::string::npos)
    {
        string_int.assign(str, start, end - start);
        ints.push_back(atoi(string_int.c_str()));
        start = end + 1;
        ++count_int;
    }
    if (start != str.size())
    {
        ints.push_back(atoi(str.c_str() + start));
        ++count_int;
    }
    return count_int;
}

int main(int argc, char *const argv[]) 
{
    std::vector<int> ints;
    std::string str = "123,456,789 ";
    std::cout << ConvertStringToInt(str, ints) << std::endl;
    for (size_t i = 0; i != ints.size(); ++i)
    {
        std::cout << ints[i] << std::endl;
    }
    return 0;
}
于 2013-08-20T13:39:49.637 回答
0

你可以让你的程序更简单:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

// output
int main()
{
    // my compiler doesn't support initializer lists yet :(
    std::vector<int> v(4);
    v[0] = 10;
    v[1] = 200;
    v[2] = 3000;
    v[3] = 40000;

    std::ofstream fout("mytestfile.txt");
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(fout, ","));
    fout.close();
    return 0;
}

// input
struct int_reader : std::ctype<char>
{
    int_reader() : std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());
        rc[','] = std::ctype_base::space;
        rc['\n'] = std::ctype_base::space;
        return &rc[0];
    }
};

int main()
{
    std::vector<int> v;
    std::ifstream fin("mytestfile.txt", std::ifstream::in);
    fin.imbue(std::locale(std::locale(), new int_reader()));
    std::copy(std::istream_iterator<int>(fin), std::istream_iterator<int>(), std::back_inserter<std::vector<int>>(v));
    fin.close();

    std::cout << "You read in:  ";
    std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
    return 0;
}
于 2013-08-20T14:08:58.547 回答
0

虽然这是手工制作而不是使用std::stringstream,但我发现它很容易理解。

std::vector<std::string> StringToVector (const std::string s,
                                         const char token)
{
  std::vector<std::string> v;

  size_t posLast = 0, pos = 0;
  while((pos = s.find(token, pos)) != std::string::npos)
  {
    if(s[pos] != s[posLast])
      v.push_back(s.substr(posLast, pos - posLast));
    posLast = ++pos;
  }
  if(s[posLast] != 0)  // If there is no terminating token found
    v.push_back(s.substr(posLast));

  return v;
}

演示各种测试用例。

于 2015-07-07T10:54:05.193 回答