How to push characters of a string into a 2d vector

I have a string s = "if man was meant to stay on the ground god would have given us roots".

I removed spaces and had "ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots".

Now I want to push the characters of the string into a 2d vector of 7rows and 8 colums.
The output should look like

ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots

I tried the following method but wasn't working.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void encryption(string s)
{
	int sq = sqrt(s.size());
	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];
	}
}
Last edited on
It looks like you just need to print a newline between rows.

22
23
24
		cout << "\n";
	}
}

That said, your code is fragile in a number of ways. What if the argument string has fewer than seven rows?
Line 14 erases the entire string every time through your loops.

Use s.erase(s.begin()); instead to erase the beginning character only.

http://www.cplusplus.com/reference/string/basic_string/erase/

Your use of std::string's operator[] hides a potential problem, you could exceed the boundaries of the string if the string's size is less than 56 characters.

The sample string you provided is 55 characters.

http://www.cplusplus.com/reference/string/basic_string/operator[]/
http://www.cplusplus.com/reference/string/basic_string/at/

Getting the first character of an empty string using operator[] returns a reference to the null character that follows the last character in the string. You got lucky you didn't end up with undefined behavior or a thrown exception.

If you used std::string's at() member function instead you'd have to wrap line 13 in a try/catch block.

Duthomhas wrote:
What if the argument string has fewer than seven rows?

The excess rows (and columns) would be filled with the null character ('\0'). So the output would have some blank rows. (Visual Studio 2019)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>

int main()
{
   std::string str { "ifmanwasmeanttostayontheground" };

   unsigned num_rows = 7;
   unsigned num_cols = 8;

   std::vector<std::vector<char>> vec;

   for (size_t rows = 0; rows < num_rows; rows++)
   {
      std::vector<char> temp;

      for (size_t cols = 0; cols < num_cols; cols++)
      {
            temp.push_back(str[0]);
            str.erase(str.begin());
      }
      vec.push_back(temp);
   }

   for (const auto& row_itr : vec)
   {
      for (const auto& col_itr : row_itr)
      {
         std::cout << col_itr;
      }
      std::cout << '\n';
   }
}

ifmanwas
meanttos
tayonthe
ground





Last edited on
Topic archived. No new replies allowed.