簡體   English   中英

錯誤:寫入數組時緩沖區溢出。 如何計算以某個字母開頭的單詞

[英]Error: buffer overrun while writing to array. How to cout the word which starts with certain letter

我需要計算以 'k; 開頭的隨機單詞; 我決定計算字數和字數(以'k'開頭); 之后我把它變成了一個數組,所以我可以有一個完整的以'k'開頭的單詞數的數組; 但問題來了,編譯器不讓代碼繼續運行,我不明白問題出在哪里; 我是初學者,所以放輕松; 這是代碼:

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <cmath>
#include <cstring>
#include <clocale>
#include <Windows.h>
using namespace std;

int main()
{
    srand(time(NULL));
    char str1[150] = "knight kevin once said that maybe that kind of process is necessary to him pushing him on and on and it is very painful he knows.";
    cout << str1 << endl;
    int sz = 1;
    int s = 1;
    int sz_count = 1;
    int s_count = 0;
    int x = 1;
    int* amounts_of_words = new int[s];
    int* array_of_K = new int[sz];

    if (str1[0] == 'k' || str1[0] == 'K') {
        array_of_K[0] = 1;
        sz++;
    }

    for (int i = 0; str1[i] != '\0'; i++) {

        if (str1[i] == ' ' || str1[i] == '.' || str1[i] == '?' || str1[i] == '!') {
            amounts_of_words[s_count] = x;
            s++;
            x++;
            if (str1[i + 1] == 'k') {
                array_of_K[sz_count] = amounts_of_words[s_count] + 1;
                sz++;
                sz_count++;
            }
            s_count++;
        }

        if (str1[i + 1] == '\0') {
            sz--;
            s--;
        }
    }

    for (int f = 0; f < sz; f++) {
        cout << array_of_K[f] << " ";
    }

    char* token;
    int randomN = rand() % 4;
    cout << randomN;
    cout << array_of_K[randomN];

    for (int i = 1; i <= s; i++) {
        if (i == 1 && i == array_of_K[randomN]) {
            token = strtok(str1, " ");
            cout << token;
            break;
        }
        else if (i == 0) {
            token = strtok(str1, " ");
        }
        else {
            token = strtok(NULL, " ");
            if (i == array_of_K[randomN]) {
                cout << token;
                break;
            }
        }
    }

    delete[] array_of_K;
    delete[] amounts_of_words;
}

首先,您可以使用std::map使您的程序更小(尺寸)和更好(通用)以下是完整的工作示例,展示了如何實現這一點:

#include <iostream>
#include <map>
#include <sstream>
int main() {
    std::string inputString = "knight kevin once said that maybe that kind of process is necessary to him pushing him on and on and it is very painful he knows.";
    std::istringstream s(inputString);
    //this map maps the char to their respective count
    std::map<char, int> charCount;
    std::string word;
    int count = 0;//this count the total number of words
    while(s >> word)
    {
        charCount[word[0]]++;
        count++;
    }
    
    
    std::cout<<"Total number of words are:"<<count<<" out of which"<<std::endl;
    for(std::pair<char, int> pairElement: charCount)
    {
        std::cout<<pairElement.second<<" starts with: "<<pairElement.first<<std::endl;
      
    }
    return 0;
}

這里查看程序。

上述程序的輸出如下:

Total number of words are:26 out of which
2 starts with: a
3 starts with: h
3 starts with: i
4 starts with: k
1 starts with: m
1 starts with: n
4 starts with: o
3 starts with: p
1 starts with: s
3 starts with: t
1 starts with: v

當人們說“慣用的 C++”時,他們的意思是這樣的:

#include <iostream>
#include <string>
#include <random>
#include <set>

// dont use : using namespace std;

// make functions!
// don't use arrays they need manual memory management with new[]/delete[]
// this is no longer the recommended way in C++
// for dynamically allocatable arrays just use std::vector!

// this is just one way of splitting a string into words.
std::vector<std::string> get_words_starting_with_letter(const std::string& string, const char letter)
{
    static const std::set<char> delimiters{ ' ', '.' }; // all those chars that are not part of a word.
    std::vector<std::string> words;
    char upper_case_letter = static_cast<char>(::toupper(letter));

    auto word_begin = string.begin();
    // use an iterator to move over the string.
    for (auto it = string.begin(); it != string.end(); ++it)
    {
        // if a non-word character is found then push back 
        // a new string beginning at word_begin adn ending at it 
        if (delimiters.find(*it) != delimiters.end())
        {
            if ((*word_begin == letter) || (*word_begin == upper_case_letter))
            {
                words.emplace_back(word_begin, it);
            }
            
            // skip delimiter
            if (it+1 != string.end())
            {
                ++it;
                word_begin = it;
            }
        }
    }

    return words;
}

int main()
{
    // srand is from "C", C++ has this:
    std::random_device rd;  // better seed then stime
    std::default_random_engine random{ rd() };

    // if something is a string then use a std::string, make it const because 
    // it is not supposed to be changed
    const std::string str1{ "knight kevin once said that maybe that kind of process is necessary to him pushing him on and on and it is very painful he knows." };

    // really do make functions to make your code readable
    auto words_with_k = get_words_starting_with_letter(str1, 'k');

    // indices in containers use std::size_t
    std::uniform_int_distribution<std::size_t> distribution(0, words_with_k.size()-1); // distributions are inclusive, so take care to substract 1 from size

    // now just output some random words
    for (std::size_t n = 0; n < 10; ++n)
    {
        auto index = distribution(random);
        std::cout << "random word beginning with a 'k' #" << n + 1 << " is : " << words_with_k.at(index) << "\n";
    }

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM