C++关于向量和 for/while 循环的新手

发布于 2024-11-26 18:28:28 字数 727 浏览 1 评论 0原文

我正在尝试制作一些东西,它将获取用户的输入行,将它们分成向量中的字符串,然后一次打印一个(每行 8 个)。 到目前为止,这就是我所得到的:

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

int main(void)
{
    using namespace std;

    vector<string> svec1;
    string temp;
    while(getline(cin, temp)) //stores lines of text in temp
    {
        if(temp.empty()) //checks if temp is empty, exits loop if so.
            break;
        stringstream ss(temp);
        string word;
        while(ss >> word) //takes each word and stores it in a slot on the vector svec1
        {
            svec1.push_back(word);
        }            
    }        
}

我一直坚持让它一次打印 8 个,我尝试过的解决方案不断出现下标超出范围的错误。

I’m trying to make something that will take lines of input from the user, separate them into strings in a vector, then print them one at a time (8 per line).
so far this is what I’ve got:

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

int main(void)
{
    using namespace std;

    vector<string> svec1;
    string temp;
    while(getline(cin, temp)) //stores lines of text in temp
    {
        if(temp.empty()) //checks if temp is empty, exits loop if so.
            break;
        stringstream ss(temp);
        string word;
        while(ss >> word) //takes each word and stores it in a slot on the vector svec1
        {
            svec1.push_back(word);
        }            
    }        
}

I’m stuck on getting it to print them 8 at a time, the solutions I’ve tried keep getting subscript out of range errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尴尬癌患者 2024-12-03 18:28:28

像这样的东西:

for(int i = 0; i < svec1.size(); i++)
{
    cout << svec1[i];
    if ((i+1) % 8 == 0)
        cout << endl;
    else
        cout << " ";
}

编辑:
上面的解决方案在末尾输出额外的空格/换行符。可以通过这样的事情来避免:

for(int i = 0; i < svec1.size(); i++)
{
    if (i == 0)
        /*do nothing or output something at the beginning*/;
    else if (i % 8 == 0)
        cout << endl; /*separator between lines*/
    else
        cout << " "; /*separator between words in line*/
    cout << svec1[i];
}

Something like this:

for(int i = 0; i < svec1.size(); i++)
{
    cout << svec1[i];
    if ((i+1) % 8 == 0)
        cout << endl;
    else
        cout << " ";
}

?

EDIT:
the solution above outputs extra space/newline at the end. It can be avoided by something like this:

for(int i = 0; i < svec1.size(); i++)
{
    if (i == 0)
        /*do nothing or output something at the beginning*/;
    else if (i % 8 == 0)
        cout << endl; /*separator between lines*/
    else
        cout << " "; /*separator between words in line*/
    cout << svec1[i];
}
古镇旧梦 2024-12-03 18:28:28

使用索引遍历向量:

for (unsigned int idx = 0; idx < svec1.size(); ++idx) {
   std::cout << svec[idx] << sep(idx); // sep(idx) is conceptual; described below
}

sep(idx) 是什么?它是在第 idxth 个单词之后打印的分隔符。这是

  • 在一行上打印了八个单词后的换行符。 idx 将为 7、15、23 等:小于 8 的整数倍。在代码中,(idx+1)%8 == 0
  • 向量中最后一项的换行符;您可能希望最后一项后跟换行符。在代码idx+1 == svec.size()中。
  • 否则一个空格。

一个简单的方法是使用三元运算符:

for (unsigned int idx = 0; idx < svec1.size(); ++idx) {
   const char * sep = (((idx+1)%8 == 0) || (idx+1 == svec.size())) ? "\n" : " ";
   std::cout << svec[idx] << sep;
}

如果你不喜欢这样,

for (unsigned int idx = 0; idx < svec1.size(); ++idx) {
   const char * sep;
   if (((idx+1)%8 == 0) || (idx+1 == svec.size())) {
      sep = "\n";
   }
   else {
      sep = " ";
   }
   std::cout << svec[idx] << sep;
}

Walk over your vector with an index:

for (unsigned int idx = 0; idx < svec1.size(); ++idx) {
   std::cout << svec[idx] << sep(idx); // sep(idx) is conceptual; described below
}

What is this sep(idx)? It is the separator to print after the idxth word. This is

  • A newline after having printed eight words on a line. idx will be 7, 15, 23, etc: One shy of an integer multiple of 8. In code, (idx+1)%8 == 0.
  • A newline for the last item in the vector; you probably want the last item to be followed with a newline. In code idx+1 == svec.size().
  • A space otherwise.

An easy way to do this is with the ternary operator:

for (unsigned int idx = 0; idx < svec1.size(); ++idx) {
   const char * sep = (((idx+1)%8 == 0) || (idx+1 == svec.size())) ? "\n" : " ";
   std::cout << svec[idx] << sep;
}

If you don't like that,

for (unsigned int idx = 0; idx < svec1.size(); ++idx) {
   const char * sep;
   if (((idx+1)%8 == 0) || (idx+1 == svec.size())) {
      sep = "\n";
   }
   else {
      sep = " ";
   }
   std::cout << svec[idx] << sep;
}
我一直都在从未离去 2024-12-03 18:28:28

通常,您使用 for 循环子句迭代向量。因此,如果您想打印 vector 的所有元素,您必须进行如下操作:

for(vector<string>::iterator it = myvec.begin(); it != myvec.end(); ++it) {
    cout << *it;
}

编辑: 正如 Vlad 已正确发布的那样,您还可以使用数组索引,在列表中效率较低,但在向量中效率相同。

Normally you iterate over a vector using a for loop clause. So if you want to print all elements of your vector<string> you have to make something like this:

for(vector<string>::iterator it = myvec.begin(); it != myvec.end(); ++it) {
    cout << *it;
}

EDIT: as Vlad has posted correctly, you can also use array indices, which are less efficient in lists, but equally efficient with vectors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文