根据输入不同的向量声明 [英] Different declaration of vectors according to input

查看:89
本文介绍了根据输入不同的向量声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

getline(cin,s);
  float n=s.size();
  if(n==6||7||8){vector< vector<string> > vec(2,  vector<string>(3));}
  else vector< vector<string> > vec(floor((sqrt(n))),  vector<string>(ceil((sqrt(n)))));



  for(int i=0;i<floor((sqrt(n)));i++)
  {
      for(int j=0;j<ceil((sqrt(n)));j++)
      {
          if(k<s.size())
          {
              vec[i][j]=s[k];
              k++;
          }
      }
  }





我正在研究加密程序我想制作一个矢量(第一次使用它),其大小应受用户输入的影响。我如何实现它?



I am working on an encryption program and I want to make a a vector(using it for the first time) whose size should be influenced by the user's input.How can I implement it?

推荐答案

你不需要'甚至担心这样的问题:矢量的大小(与数组不同)可以随时改变。请参阅:

http://www.cplusplus.com/reference/vector/vector/ [ ^ ]。



Modifiers:部分列出的9个函数可以在已经创建并初始化之后更改矢量实例的大小。



-SA
You needn't even worry about such a problem: the size of the vector (unlike the array) can be changed at any time. Please see:
http://www.cplusplus.com/reference/vector/vector/[^].

The 9 functions listed at the the section "Modifiers:" can change the size of a vector instance, after it is already created and initialized.

—SA


代码中有一些错误。



首先,条件

There are a few errors in your code.

First, the condition
if (n==6||7||8) 

始终为真。您可能想要的是

is always true. What you probably meant is

if (n==6 || n==7 || n==8)



其次,变量 vec 在第一个 if 块内声明,然后在<$ c $中再次声明c> else 阻止,但它在任一块的末尾都超出范围 - 你无法通过这些块访问它,你的编译器应该给你一个错误信息。

第三,您将 vec 定义为 vector< vector< string>> ,因此表达式 vec [i] [j] 表示字符串,而 s [k] 是一个字符。类型不匹配!虽然这可能不会导致编译器错误,但数据不会以预期的方式存储。您可能应该将 vec 声明为 vector< vector< char>>



至于你的问题,有两种方法可以处理它:第一种是在运行时动态分配向量。第二个是简单地将该任务留给类 vector ,正如SA在解决方案1中指出的那样。也就是说,通过索引运算符访问vector元素将自动增加向量尺寸如果需要。但同样不适用于字符串字符串,通过索引操作符不会自动调整该字符串的大小!如上所述,将类型更改为 vector< vector< char>> 的另一个原因。


Second, the variable vec is declared inside the first if block and then again in the else block, but it runs out of scope at the end of either block - you cannot access it past these blocks and your compiler should give you an error message.
Third, you defined vec as vector<vector<string>>, so the expression vec[i][j] denotes a string, whereas s[k] is a character. The types don't match! While this may not result in a compiler error, the data will not be stored in the expected way. You probably should declare vec as vector<vector<char>>

As to your problem, there are two ways to deal with it: the first is to allocate the vector dynamically, at run time. The second is to simply leave that task to the class vector, as SA pointed out in solution 1. That said, accessing a vector element through the index operator will automagically increase the vector size if required. But the same is not true for strings string through the index operator will not automagically adapt the size of that string! Another reason to change the type to vector<vector<char>> as suggested above.


这篇关于根据输入不同的向量声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆