【问题标题】:Element access (get and put) using std::vector in C++在 C++ 中使用 std::vector 访问元素(获取和放置)
【发布时间】:2014-01-14 18:35:03
【问题描述】:

我正在尝试使用矢量来操作文件 (.txt) 的元素 - 例如 push_backaccessing [] 等。我想对从文件中检索到的元素执行简单的编码,但我部分成功了最终的结果不是我所期望的。

这是文件内容(示例):

datum
-6353
argo

我想在这里执行的是读取向量的每个元素(有点二维)并用它们各自的ASCII 代码对它们进行编码。

这是我的代码(到目前为止):

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

    using namespace std;

    int main () 
    {
        std::string word;
        //std::vector<std::vector <std::string> > file;
        std::vector<std::string> file;

        std::ifstream in( "test.txt" );

        while (in >> word)
        {
            file.push_back(word);
        }

        for (size_t i=0; i<file.size(); i++)
        {
           for (int j=0;j<file[i].size();j++)
           {
               //cout<<i<<","<< j<<endl;
               cout<<((int)file[i][j])<<",";
           }
        }

        in.close();

  return 0;
}

当前输出(对于上述代码):

100,97,116,117,109,45,54,51,53,51,97,114,103,111,
RUN FINISHED; exit value 0; real time: 0ms; user: 0ms; system: 0ms

预期输出:

100,97,116,117,109
45,54,51,53,51
97,114,103,111

请指导我实现这一目标。

【问题讨论】:

    标签: c++ file vector


    【解决方案1】:

    在第一个循环后尝试 cout

    for (size_t i=0; i<file.size(); i++)
    {
       for (int j=0;j<file[i].size();j++)
       {
           //cout<<i<<","<< j<<endl;
           cout<<((int)file[i][j])<<",";
       }
      **cout << endl;**
    }
    

    【讨论】:

    【解决方案2】:

    您已经选择了一个解决方案,但我想我会把它放在那里并使用 C++ 语言。 由于您在上述问题中似乎有 #include &lt;iterator&gt; 并且您包含标签 C++,我认为使用迭代器等更优雅的解决方案将是值得的帖子 - 尽管您已经选择了答案。

    template <typename T>
    void printToScreen (std::vector<T> myVec, char delim = ',')
    {
        typename std::vector<vector<T>>::const_iterator vit_x;
        typename std::vector<T>::const_iterator vit_y;
        for (vit_x = myVec.begin(); vit_x < myVec.end(); vit_x++) {
            for (vit_y = vit_x->begin(); vit_y < vit_x->end(); vit_y++) {
                std::cout << *vit_y << delim;
            }
            // remove last delimiter and add new line to console
            std::cout << "\b \n";
        }
    }
    

    然后在您的 main 函数中,您可以执行以下操作:

    printToScreen (file);
    

    或者如果你想要另一个分隔符,例如'.'

    printToScreen (file,'.');
    

    如果你有C++11,功能可以进一步简化

    template <typename T>
    void printToScreen(const std::vector<std::vector<T>> &myVec, char delim = ',')
    {
        for (auto i : myVec) {
            for (auto val : i) {
                std::cout << val << delim;
            }
            std::cout << "\b \n";
        }
    }
    

    可以使用C++11 lambda 函数进一步简化这一过程。这只是 2D 的示例。甚至可以将 delim 参数更改为字符串,因此分隔符是一个多字符序列,然后为字符串的大小生成 '\b' 特殊字符。

    测试:

    std::vector<std::vector<int>> d1{{1,2,3},{2,3,4,7,6}, {3,4,5,2,1}};
    std::vector<std::vector<std::string>> d2{{"Testing","function","prettyToScreen"},{"I'm","making","use of","the"}, {"C++","language"}};
     printToScreen(d1);
     std::cout << endl;
     printToScreen(d2, ' ');
    

    输出:

    1,2,3
    2,3,4,7,6
    3,4,5,2,1
    
    Testing function prettyToScreen 
    I'm making use of the 
    C++ language 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-23
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-05
      • 1970-01-01
      相关资源
      最近更新 更多