例如,我有句“我是Piet”。我希望以I [0],我[1] Piet [2]的方式将这句话填入数组。以下是我制作的代码。问题是句子填充在数组的每个元素中。
#include <iostream>
#include <string>
using namespace std;
// function to populate my array
void populateMyArray(string*myArray, string sentence, int size)
{
for (int i = 0; i < size; i++)
{
*myArray = sentence;
myArray++;
}
}
// function to count works in the sentence
int countWords(string x)
{
int Num = 0;
char prev = ' ';
for (unsigned int i = 0; i < x.size(); i++) {
if (x[i] != ' ' && prev == ' ') Num++;
prev = x[i];
}
return Num;
}
int main()
{
string sentence1;
cout << "Please enter a line of text:\n";
getline(cin, sentence1);
int nWords1 = countWords(sentence1);
string *arr1 = new string[nWords1];
populateMyArray(arr1, sentence1, nWords1); //populate array1
for (int i = 0; i < nWords1; i++)
{
cout << "sentence one: " << arr1[i] << "\n";
}
system("PAUSE");
}
您可以使用向量来保存数据,每次在两个单词之间使用空格时,您将每个字符串类型存储到向量数组中
#include<bits/stdc++.h>
using namespace std;
main()
{
string s;
getline(cin,s);
vector<string> ss;
string temp = "";
s +=" ";
for(int i = 0 ; i < s.size();i ++){
if(s[i] != ' ')
temp += s[i];
else{
ss.push_back(temp);
temp = "";
}
}
for(int i = 0 ; i < ss.size();i ++)
cout << ss[i] <<" ";
}
而不是使用数组,而是使用std::vector
。这样,在单词或句子太长的情况下,您不必担心可变字大小或溢出任何内容。相反,你可以做这样的事情:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
int main() {
// Get all words on one line
std::cout << "Enter words: " << std::flush;
std::string sentence;
getline(std::cin, sentence);
// Parse words into a vector
std::vector<std::string> words;
std::string word;
std::istringstream iss(sentence);
while( iss >> word ) {
words.push_back(word);
}
// Test it out.
for(auto const& w : words) {
std::cout << w << std::endl;
}
}
对于I like cats and dogs equally
的一个例子,你将有:words[0] = I
,words[1] = like
等。
如果我理解正确,你试图将输入句分成单词。
你可以这样做:
void populateMyArray(string *myArray, string sentence, int size)
{
int firstCharIndex = -1;
char prev = ' ';
for (unsigned int i = 0; i < sentence.size(); i++) {
// Find the first character index of current word
if (sentence[i] != ' ' && prev == ' ') {
firstCharIndex = i;
}
// Check if it's the end of current word
// and get substring from first to last index of current word
else if (sentence[i] == ' ' && prev != ' ') {
*myArray = sentence.substr(firstCharIndex, i - firstCharIndex);
myArray++;
}
prev = sentence[i];
}
// For the last word
if (firstCharIndex != -1 && sentence[sentence.size() - 1] != ' ') {
*myArray = sentence.substr(firstCharIndex, sentence.size() - firstCharIndex);
}
}
如何像程序员一样思考。
我们需要的第一件事是开头的定义,一个单词的单词和结尾。您可能认为单词的开头是一个以空格开头的非空格,而单词的结尾是一个非空格,后跟一个空格。但这些定义是错误的,因为它们忽略了字符串开头或结尾处单词的可能性。单词开头的正确定义是字符串开头的非空格或空格开头的非空格。类似地,单词的结尾是字符串末尾的非空格或非空格,后跟空格。
现在我们有两个函数捕获它们的定义。将复杂问题分解成更小的部分是非常重要的,而这样做的方法是编写函数(或类)。
bool beginning_of_word(string str, int index)
{
return str[index] != ' ' && (index == 0 || str[index - 1] == ' ');
}
bool end_of_word(string str, int index)
{
return str[index] != ' ' && (index == str.size() - 1 || str[index + 1] == ' ');
}
现在我们越来越近了,但我们仍然需要找到下一个单词开头或单词的下一个结尾的想法,这样我们就可以循环查找每个单词一次一个的句子。以下是查找单词的下一个开头和下一个结尾的两个函数。它们从给定的索引开始,找到下一个索引,它是单词的开头或结尾。如果没有找到这样的索引,则返回-1。
int next_beginning_of_word(string str, int index)
{
++index;
while (index < str.size())
{
if (beginning_of_word(str, index))
return index; // index is a start of word so return it
++index;
}
return -1; // no next word found
}
int next_end_of_word(string str, int index)
{
++index;
while (index < str.size())
{
if (end_of_word(str, index))
return index; // index is an end of word so return it
++index;
}
return -1; // no next word found
}
现在我们有一种方法可以循环查看句子中的单词,我们已准备好编写主循环。我们使用substr
来破坏句子中的单词,substr
将两个参数作为单词开头的索引和单词的长度。我们可以通过从末尾减去开头并添加一个来获得单词的长度。
int populateMyArray(string* array, string sentence)
{
// find the first word
int start = next_beginning_of_word(sentence, -1);
int end = next_end_of_word(sentence, -1);
int count = 0;
while (start >= 0) // did we find it?
{
// add to array
array[count] = sentence.substr(start, end - start + 1);
++count;
// find the next word
start = next_beginning_of_word(sentence, start);
end = next_end_of_word(sentence, end);
}
return count;
}
现在为了额外的功劳,我们可以使用countWords
重写next_beginning_of_word
int countWords(string sentence)
{
int start = next_beginning_of_word(sentence, -1);
int count = 0;
while (start >= 0)
{
++count;
start = next_beginning_of_word(sentence, start);
}
return count;
}
注意countWords
和populateMyArray
函数的相似性,循环非常相似。这应该会给你信心。
这是程序员工作的方式,当你遇到一个太复杂而无法处理的问题时,将其分解成更小的部分。