在 C++ 中显示字符串向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17938166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying a vector of strings in C++
提问by RudolphRedNose
I'm sorry if this is a repeat question but I already tried to search for an answer and came up empty handed. So basically I just want to add strings (single words) to the back of a vector and then display the stored strings as a single string. I am quite the rookie.
如果这是一个重复的问题,我很抱歉,但我已经尝试寻找答案并空手而归。所以基本上我只想将字符串(单个单词)添加到向量的后面,然后将存储的字符串显示为单个字符串。我是菜鸟。
#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;
}
Why doesn't this work?
为什么这不起作用?
EDIT
编辑
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;
}
So my new program works. It just puts values at the back of a vector and prints them out 8 words to a line. I know there's easier ways but I'm just learning vectors and I'm going in baby steps. Thanks for the help guys.
所以我的新程序有效。它只是将值放在向量的后面,并将它们打印成一行 8 个字。我知道有更简单的方法,但我只是在学习向量,而且我正在逐步进行。谢谢你们的帮助。
回答by Oleksiy
Because userString is empty. You only declare it
因为 userString 是空的。你只声明它
vector<string> userString;
but never add anything, so the for loop won't even run.
但永远不要添加任何东西,所以 for 循环甚至不会运行。
回答by juanchopanza
Your vector<string> userString
has size 0
, so the loop is never entered. You could start with a vector of a given size:
你vector<string> userString
有 size 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] + " ";
}
although it is not clear why you need the vector at all:
虽然不清楚为什么你需要向量:
string word;
string sentence;
for (int i = 0; i < 10; ++i)
{
cin >> word;
sentence += word + " ";
}
If you don't want to have a fixed limit on the number of input words, you can use std::getline
in a while
loop, checking against a certain input, e.g. "q"
:
如果您不想对输入单词的数量有固定限制,您可以std::getline
在while
循环中使用,检查某个输入,例如"q"
:
while (std::getline(std::cin, word) && word != "q")
{
sentence += word + " ";
}
This will add words to sentence
until you type "q".
这将添加单词,sentence
直到您键入“q”。
回答by Mahesh
You have to insert the elements using the insertmethod present in vectors STL, check the below program to add the elements to it, and you can use in the same way in your program.
您必须使用向量 STL 中存在的插入方法插入元素,检查以下程序以向其中添加元素,您可以在程序中以相同的方式使用。
#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;
}
回答by kfsone
You ask two questions; your title says "Displaying a vector of strings", but you're not actually doing that, you actually build a single string composed of all the strings and output that.
你问两个问题;你的标题说“显示一个字符串向量”,但你实际上并没有这样做,你实际上构建了一个由所有字符串组成的单个字符串并输出它。
Your question body asks "Why doesn't this work".
您的问题主体会问“为什么这行不通”。
It doesn't work because your for loop is constrained by "userString.size()" which is 0, and you test your loop variable for being "userString.size() - 1". The condition of a for() loop is tested before permitting execution of the first iteration.
它不起作用,因为您的 for 循环受“userString.size()”约束,即 0,并且您测试循环变量为“userString.size() - 1”。在允许执行第一次迭代之前测试 for() 循环的条件。
int n = 1;
for (int i = 1; i < n; ++i) {
std::cout << i << endl;
}
will print exactly nothing.
将完全不打印。
So your loop executes exactly no iterations, leaving userString and sentence empty.
因此,您的循环完全不执行迭代,将 userString 和句子留空。
Lastly, your code has absolutely zero reason to use a vector. The fact that you used "decltype(userString.size())" instead of "size_t" or "auto", while claiming to be a rookie, suggests you're either reading a book from back to front or you are setting yourself up to fail a class.
最后,您的代码完全没有理由使用向量。您使用“decltype(userString.size())”而不是“size_t”或“auto”这一事实,同时声称自己是新手,这表明您要么是从后到前阅读一本书,要么是在设置自己不及格。
So to answer your question at the end of your post: It doesn't work because you didn't step through it with a debugger and inspect the values as it went. While I say it tongue-in-cheek, I'm going to leave it out there.
因此,在您的帖子末尾回答您的问题:它不起作用,因为您没有使用调试器逐步完成并检查值。虽然我半开玩笑地说,但我要把它放在那里。
回答by lukai
vector.size()
returns the size of a vector. You didn't put any string in the vector before the loop , so the size of the vector is 0. It will never enter the loop. First put some data in the vector and then try to add them. You can take input from the user for the number of string user wants to enter.
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;
}
another thing, actually you don't have to use a vector to do this.Two strings can do the job for you.
另一件事,实际上您不必使用向量来执行此操作。两个字符串可以为您完成这项工作。
#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;
}
and if you want to enter string until the user wish , code will be like this:
如果您想在用户希望之前输入字符串,代码将如下所示:
#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;
}