如何将字符串的字符推入二维向量[关闭]

Posted

技术标签:

【中文标题】如何将字符串的字符推入二维向量[关闭]【英文标题】:How to push characters of a string into a 2d vector [closed] 【发布时间】:2019-08-28 00:13:15 【问题描述】:

我有一个字符串s = "if man was meant to stay on the ground god would have given us roots"

我删除了空格并拥有"ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots"

现在我想将字符串的字符推入一个 7 行 8 列的二维向量。 输出应如下所示:

ifmanwas 
meanttos 
tayonthe 
groundgo 
dwouldha 
vegivenu 
sroots

我尝试了以下方法,但没有成功。

void encryption(string s)

    const int rows = 7;
    const int colums = 8;

    vector<vector<char>> v;
    for (int i = 0; i < rows; i++)
    
        vector<char>temp;
        for (int j = 0; i < colums; j++)
        
            temp.push_back(s[0]);
            s.erase(0);
        
        v.push_back(temp);
    
    for (int i = 0; i < v.size(); i++)
    
        for (int j = 0; j < v[i].size(); j++)
            cout << v[i][j];
    

【问题讨论】:

强烈建议检查您是否没有用完string s.erase(0); -- 你为什么要擦除字符?从问题描述来看,擦除字符的原因是什么?这只是在循环中将一系列字符复制到另一个缓冲区中,并指定和更新开始和结束索引/迭代器。无需擦除。 请解释您所说的“没有工作”是什么意思 为什么是vector&lt;vector&lt;char&gt;&gt;?为什么不是vector&lt;std::string&gt;?这样你就不会一次处理一个角色。为什么限制为 7 行?如果字符串很短,或者字符串超过 7 行怎么办? for (int j = 0; i &lt; colums; j++) -- 看到这个循环有什么问题吗?喜欢使用i 而不是j 【参考方案1】:

您的代码有几个问题:

1) 此处循环中使用了错误的索引变量: for (int j = 0; i &lt; colums; j++)

2) std::vector::erase 函数将迭代器作为参数,而不是位置。

3) 即使你修复了 1) 中提到的循环,你也不会检查你是否在字符串的末尾。

4) 你的最终输出没有输出回车,使得输出看起来“错误”。

给定您使用的字符串,进行适当的更改,代码可能如下所示:

#include <string>
#include <vector>
#include <iostream>

using namespace std;

void encryption(string s)

    const int rows = 7;
    const int colums = 8;

    vector<vector<char>> v;
    for (int i = 0; i < rows; i++)
    
        vector<char>temp;
        for (int j = 0; j < colums && !s.empty(); j++)  // <-- Note we stop the loop if the string is empty.
        
            temp.push_back(s[0]);
            s.erase(s.begin());  // <-- This erases the first character
        
        v.push_back(temp);
    
    for (size_t i = 0; i < v.size(); i++)
    
        for (size_t j = 0; j < v[i].size(); j++)
            cout << v[i][j];
        cout << "\n";    // <-- You are missing this in your output
    


int main()

    encryption("ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots");

输出:

ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots

鉴于此,这是非常低效的,因为您正在擦除字符串的第一个字符。无需为此删除字符。

这里是使用std::vector&lt;std::string&gt;while 循环的替代解决方案,用于跟踪字符串中的开始和结束迭代器:

#include <string>
#include <vector>
#include <algorithm>
#include <iostream>

void encryption(std::string s)

    const size_t columns = 8;

    std::vector<std::string> v;

    // start at beginning of string
    auto startIter = s.begin();

    // point to either 8 characters after the start, or the end of the
    // string, whichever comes first
    auto endIter = startIter + std::min(columns, static_cast<size_t>(std::distance(startIter, s.end())));
    while (true)
    
        // just push on a string using the start and end iterators
        v.push_back(startIter, endIter);

        // if the end iterator is at the end of the string, quit
        if (endIter == s.end())
            break;

        // move the start to the end
        startIter = endIter;

        // move the end to either 8 characters after the start, or the
        // end of the string, whichever comes first
        endIter = startIter + std::min(columns, static_cast<size_t>(std::distance(startIter, s.end())));
    

    // Output the results
    for (auto& vrow : v)
        std::cout << vrow << "\n";


int main()

    encryption("ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots");

输出:

ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots

【讨论】:

感谢您的详细解释。

以上是关于如何将字符串的字符推入二维向量[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

将字符串列表插入二维向量

为啥我不能将整数向量推入 C++ 中的二维整数向量?

如何从输入字符串制作二维向量?

无法将字符串分配给向量的向量

如何将字符推入字符串堆栈?

将字符串向量转换为双二维数组向量