在C++中显示字符串的矢量

Displaying a vector of strings in C++

本文关键字:字符串 显示 C++      更新时间:2023-10-16

如果这是一个重复的问题,我很抱歉,但我已经试着寻找答案了,但一无所获。所以基本上我只想在向量的后面添加字符串(单个单词),然后将存储的字符串显示为单个字符串。我是个新手。

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;

int main(int a, char* b [])
{
    vector<string> userString;      
    string word;        
    string sentence = "";           
    for (decltype(userString.size()) i = 0; i <= userString.size() - 1; i++)
    {
        cin >> word;
        userString.push_back(word);
        sentence += userString[i] + " ";
    }
    cout << sentence;
    system("PAUSE");
    return 0;
}

为什么不起作用?

编辑

int main(int a, char* b [])
{
    cout << "Enter a sequence of words. Enter '.' n";
    vector<string> userString;      
    string word;                    
    string sentence = "";           /
    int wordCount = 0;
    while (getline(cin, word))
    {
        if (word == ".")
        {
            break;
        }
        userString.push_back(word);
    }
    for (decltype(userString.size()) i = 0; i <= userString.size() - 1; i++)
    {
        sentence += userString[i] + " ";
        wordCount += 1;
        if (wordCount == 8)
        {
            sentence = sentence + "n";
                    wordCount = 0;
        }
    }
    cout << sentence << endl; 
    system("PAUSE");
    return 0;
}

所以我的新程序有效。它只是把值放在向量的后面,然后把它们打印成一行8个单词。我知道有更简单的方法,但我只是在学习矢量,我正在循序渐进。谢谢你们的帮助。

因为userString为空。你只申报

vector<string> userString;     

但是永远不要添加任何内容,所以for循环甚至不会运行。

您的vector<string> userString的大小为0,因此永远不会进入循环。你可以从一个给定大小的向量开始:

vector<string> userString(10);      
string word;        
string sentence;           
for (decltype(userString.size()) i = 0; i < userString.size(); ++i)
{
    cin >> word;
    userString[i] = word;
    sentence += userString[i] + " ";
}

尽管还不清楚你为什么需要这个矢量:

string word;        
string sentence;           
for (int i = 0; i < 10; ++i)
{
    cin >> word;
    sentence += word + " ";
}

如果你不想对输入单词的数量有固定的限制,你可以在while循环中使用std::getline,检查特定的输入,例如"q":

while (std::getline(std::cin, word) && word != "q")
{
    sentence += word + " ";
}

这将在sentence中添加单词,直到您键入"q"为止。

您必须使用矢量STL中的insert方法插入元素,检查下面的程序将元素添加到其中,然后您可以在程序中以相同的方式使用。

#include <iostream>
#include <vector>
#include <string.h>
int main ()
{
  std::vector<std::string> myvector ;
  std::vector<std::string>::iterator it;
   it = myvector.begin();
  std::string myarray [] = { "Hi","hello","wassup" };
  myvector.insert (myvector.begin(), myarray, myarray+3);
  std::cout << "myvector contains:";
  for (it=myvector.begin(); it<myvector.end(); it++)
    std::cout << ' ' << *it;
    std::cout << 'n';
  return 0;
}

您会问两个问题;你的标题是"显示字符串向量",但你实际上并没有这么做,你实际上构建了一个由所有字符串组成的单个字符串并输出它。

你的问题主体会问"为什么这不起作用"。

它不起作用,因为for循环受到"userString.size()"的约束,该值为0,并且您测试循环变量是否为"userString.size()-1"。在允许执行第一次迭代之前,将测试for()循环的条件。

int n = 1;
for (int i = 1; i < n; ++i) {
    std::cout << i << endl;
}

将完全不打印任何内容。

因此,您的循环完全不执行迭代,只留下userString和句子为空。

最后,您的代码绝对没有理由使用向量。事实上,当你声称自己是一名新手时,你使用了"decltype(userString.size())"而不是"size_t"或"auto",这表明你要么是在从头读一本书,要么是在设置自己的课堂不及格。

因此,在文章结尾回答你的问题:它不起作用,因为你没有使用调试器来遍历它,并在它运行时检查值。虽然我是开玩笑地说,但我要把它放在一边。

vector.size()返回向量的大小。循环之前没有在向量中放入任何字符串,因此向量的大小为0。它永远不会进入循环。首先将一些数据放入向量中,然后尝试将它们相加。您可以从用户那里输入用户想要输入的字符串数。

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;

int main(int a, char* b [])
{
    vector<string> userString;
    string word;
    string sentence = "";
    int SIZE;
    cin>>SIZE;    //what will be the size of the vector
    for (int i = 0; i < SIZE; i++)
    {
        cin >> word;
        userString.push_back(word);
        sentence += userString[i] + " ";
    }
    cout << sentence;
    system("PAUSE");
    return 0;
}

另一件事,实际上你不需要使用向量来做这件事。两根绳子可以为你做这项工作。

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;

int main(int a, char* b [])
{
   // vector<string> userString;
    string word;
    string sentence = "";
    int SIZE;
    cin>>SIZE;    //what will be the size of the vector
    for (int i = 0; i < SIZE; i++)
    {
        cin >> word;
        sentence += word+ " ";
    }
    cout << sentence;
    system("PAUSE");
    return 0;
}

如果你想输入字符串,直到用户愿意,代码将是这样的:

#include <iostream>
#include <vector>
#include <string>
#include <cctype>
using namespace std;

int main(int a, char* b [])
{
   // vector<string> userString;
    string word;
    string sentence = "";
    //int SIZE;
    //cin>>SIZE;    //what will be the size of the vector
    while(cin>>word)
    {
        //cin >> word;
        sentence += word+ " ";
    }
    cout << sentence;
  //  system("PAUSE");
    return 0;
}

不过,这段代码可以在没有任何修改的情况下工作。

来源:https://gist.github.com/lucianmachado/9a26d5745497ffe5d054

#include <glob.h>
#include <vector>
#include <string>
inline std::vector<std::string> glob(const std::string& pat){
    using namespace std;
    glob_t glob_result;
    glob(pat.c_str(),GLOB_TILDE,NULL,&glob_result);
    vector<string> ret;
    for(unsigned int i=0;i<glob_result.gl_pathc;++i){
        ret.push_back(string(glob_result.gl_pathv[i]));
    }
    globfree(&glob_result);
    return ret;
}

像一样使用

glob("pattern");

"图案";也可以是字符串.c_str()。它还返回字符串,以便您也可以在字符串变量中捕获它。