C++ 如何将字符串中的字符推送到二维向量中

C++ 如何将字符串中的字符推送到二维向量中,c++,vector,data-structures,2d-vector,C++,Vector,Data Structures,2d Vector,我有一根弦s=如果人注定要留在地面上,上帝会给我们根 我去掉了空间,让Ifman留在地面上,上帝会给我根 现在我想把字符串的字符放入一个7行8列的2d向量中。 输出应该如下所示: ifmanwas meanttos tayonthe groundgo dwouldha vegivenu sroots 我尝试了下面的方法,但不起作用 void encryption(string s) { const int rows = 7; const int colums = 8

我有一根弦s=如果人注定要留在地面上,上帝会给我们根

我去掉了空间,让Ifman留在地面上,上帝会给我根

现在我想把字符串的字符放入一个7行8列的2d向量中。 输出应该如下所示:

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];
    }
}

您的代码有几个问题:

此处循环中使用了1个错误的索引变量: 对于int j=0;i 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");
}
鉴于此,这是一个非常低效的方法,因为您正在擦除字符串的第一个字符。没有必要为此删除字符

下面是一个使用std::vector的替代解决方案,以及一个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");
}

强烈建议检查字符串是否已用完。s.erase0;-为什么要删除字符?根据问题描述,删除字符的原因是什么?这只是将一系列字符复制到循环中的另一个缓冲区中,并指定和更新起始和结束索引/迭代器。不需要擦除。请解释你所说的不工作是什么意思为什么是向量?为什么不是向量?那么你就不会一次处理一个角色了。为什么限制为7行?如果字符串很短,或者字符串的跨度超过7行,该怎么办;i#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