指向另一个数组值的C ++指针数组

时间:2016-01-02 13:29:30

标签: c++ arrays pointers

我想制作一系列词语:" Tom"," Mike"," Tamara"," Nik"。 ..我想让用户可以输入例如3号,然后随机返回长度为3的单词,这样就可以了(" Tom"或" Nik& #34)。我认为这是通过指针完成的,但我不知道如何。单词应根据其长度存储在不同的数组中。并且使用指针,您将指向每个阵列(" Tom"," Nik"在同一阵列" Tamara"在不同阵列和" Mike"在不同的数组等等......因为它们的长度不一样)。有人可以帮忙吗?

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

    void IzpisPolja(char **polje,int velikost){

    int tab[100];

    for (int i=0; i<velikost; i++) {
        cout<<polje[i]<<endl;
        char *zacasni;


        tab[i] = strlen(polje[i]);
       // cout<<tab[i]<<endl;   
    }
}

int main(){

    const int size = 4;
    char* tabelaOseb[size] = {"Tom", "Mike","Tamara","Nik"};

    IzpisPolja(tabelaOseb,size);

    return 0;
}

3 个答案:

答案 0 :(得分:0)

你想有效地做吗?将它们存储在单独的阵列中会增加搜索时间,但也会增加插入,删除的复杂性。

否则,您只需计算数组中n个长度字的实例数,然后生成随机数并返回i个。{/ p>

同时建议使用std::vector

const string* getRandNameOfLength(const string* arr, 
                        const int arrlen, 
                        const int length)
{
    int num = 0, j, i;

    // Counting number of such names
    for (i = 0; i < arrlen; ++i)
    {
        if (arr[i].size() == length)
            num++;
    }

    // No such name found
    if (num == 0)
        return NULL;

    j = rand() % num;

    // Returning random entry of given length
    for (i = 0; i < arrlen; ++i)
    {
        if (arr[i].size() == length && j-- == 0)
            return &arr[i];
    }

    // Function shouldn't get here
    return NULL;
}

答案 1 :(得分:0)

#include <iostream>
#include <vector>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace std;

const char* return_rand_name(const char** names, size_t length)
{
    std::vector<size_t> indexes;
    for(int i=0; names[i][0] != 0; ++i)
        if(strlen(names[i]) == length)
            indexes.push_back(i);
    if(indexes.size()==0)
        return NULL;
    return names[indexes[rand()%indexes.size()]];
}

int main()
{
    srand(time(NULL));
    const char* names[] = {"Alex","Tom","Annie","Steve","Jesus","Leo","Jerry",""};
    std::cout << return_rand_name(names, 3) << std::endl;
    return 0;
}

如果您想使用strlen等功能,请添加<cstring>,而不是<string>(其中包含类模板std::string(您应该在C ++中使用) char *)))

答案 2 :(得分:0)

当然,您可以使用原始指针来执行任务,但您也可以开始使用语言(引用,迭代器和智能指针)和C ++标准库可以提供的许多更安全的工具。

我将向您展示一个完整的程序,可以使用上传者(std::mapstd::lower_bound)和算法(如main())来完成您的工作,这些算法可以真正简化您的工作一旦理解。 请注意,作为一种学习练习(对于我们两个人来说),我已经使用了很多&#34; new&#34;尽我所能,即使可能没有必要或方便。阅读评论以便更好地理解。

单词在课堂中存储和管理,而与用户的互动则在#include <iostream> #include <string> #include <vector> #include <map> #include <limits> #include <algorithm> #include <random> // for mt19937, uniform_int_distribution #include <chrono> // for high_resolution_clock size_t random_index( size_t a, size_t b ) { // Initialize Random Number Generator Engine as a static variable. - Since c++11, You can use those instead of old srand(time(NULL)) static std::mt19937 eng{static_cast<long unsigned int>(std::chrono::high_resolution_clock::now().time_since_epoch().count())}; // use the RNG to generate random numbers uniformly distributed in a range return std::uniform_int_distribution<size_t>(a,b)(eng); } using svs_t = std::vector< std::string >; // I store the words with equal length in a std::vector of std::string // like typedef, I'll use svs_t instead of std::vector<std::string> auto string_less_then = [] (const std::string & a, const std::string & b) -> bool { return a.compare(b) < 0; }; // A lambda function is a mechanism for specifying a function object, its primary use is to specify a simple // action to be performed by some function. I'll use it to compare two string and return true only if a<b class word_table { std::map< size_t, svs_t > words; // std::map store elements formed by a combination of a key value and a mapped value, sorted by key // I'll use word's length as a key for svs_t values public: word_table() {} word_table( std::initializer_list<std::string> vs ) { insert_words(vs); } void insert_words( svs_t vs ) { for ( auto && s : vs ) add_word(s); // loop for each value in vs, "auto" let the compiler infer the right type of the variable } bool add_word( std::string s ) { // I choose to keep the vector sorted and with unique elements size_t sl = s.length(); if ( sl > 0 ) { auto & v = words[sl]; // If sl doesn't match the key of any element in the map, a new element is created // lower_bound return an iterator that poins to the first element in range (begin,end) auto it = std::lower_bound(v.begin(), v.end(), s, string_less_then); // which does not compare less than s // I pass the compare function as a lambda if ( it != v.end() && it->compare(s) == 0 ) return false; // Already present, duplicates not allowed v.insert(it, s); // Not the most efficient way, but you seem focused on the random access part return true; } return false; } bool remove_word( std::string s) { size_t sl = s.length(); if ( sl > 0 ) { auto itvw = words.find(sl); // first find the right element in the map, using the string length as a key, but if word is found if ( itvw == words.end() ) return false; // an iterator to the element following the last element of the container is returned auto & v = itvw->second; // In a map the elements are stored in pairs, first is the key, second the value auto it = std::lower_bound(v.begin(), v.end(), s, string_less_then); if ( it != v.end() && it->compare(s) == 0 ) { v.erase(it); if ( v.empty() ) words.erase(itvw); return true; } } return false; } std::string get_random_word( size_t length ) { if ( length == 0 ) return ""; auto itvw = words.find(length); if ( itvw == words.end() || itvw->second.empty() ) return ""; return itvw->second[random_index(0, itvw->second.size() - 1)]; } void show_all() { for ( auto && i : words ) { std::cout << " "; for (auto && w : i.second ) { std::cout << w << ' '; } std::cout << '\n'; } } }; constexpr size_t ss_max = std::numeric_limits<std::streamsize>::max(); namespace opt { enum options { wrong = -1, exit, show, random, add, remove, menu }; } class menu { std::map<int,std::string> opts; public: menu( std::initializer_list<std::pair<int,std::string>> il ) { for ( auto && i : il ) opts.insert(i); } void show() { std::cout << "\nYou can choose among these options:\n\n"; for ( auto && i : opts ) { std::cout << " " << i.first << ". " << i.second << ".\n"; } } }; int main() { word_table names({"Tom", "Mike","Tamara","Robert","Lenny","Nick","Alex","Sue","Irina","Beth","Anastacia","Bo"}); int choise = opt::exit; menu menu_options { {opt::exit, "Exit program"}, {opt::show, "Show all stored names"}, {opt::random, "Show a random name"}, {opt::add, "Add a new name"}, {opt::remove, "Remove a name"} }; menu_options.show(); do { std::cout << "\nPlease, enter a number (" << opt::menu << " to show again all options): "; std::cin >> choise; if ( std::cin.fail() ) { // the user enter something that is not a number choise = opt::wrong; std::cin.clear(); std::cin.ignore(ss_max,'\n'); } if ( std::cin.eof() ) break; // use only if you are redirecting input from file std::string str; switch ( choise ) { case opt::exit: std::cout << "\nYou choose to quit, goodbye.\n"; break; case opt::show: std::cout << "\nAll the stored names, classified by word\'s length:\n\n"; names.show_all(); break; case opt::random: size_t l; std::cout << "Please, enter the length of the name: "; std::cin >> l; if ( std::cin.good() ) { std::string rs = names.get_random_word(l); if ( rs == "" ) { std::cout << "\nNo name of length " << l << " has been found.\n"; } else { std::cout << "\n " << rs << '\n'; } } break; case opt::add: std::cout << "Please, enter the name You want to add: "; std::cin >> str; // read a string from cin, you can write more than a word (separeted by spaces) std::cin.ignore(ss_max,'\n'); // but only the first is stored if ( names.add_word(str) ) { std::cout << "\n The name " << str << " has been successfully added.\n"; } else { std::cout << "\n No name has been added"; if ( str != "" ) std::cout << ", "<< str << " is already present.\n"; else std::cout << ".\n"; } break; case opt::remove: std::cout << "Please, enter the name You want to remove: "; std::cin >> str; if ( names.remove_word(str) ) { std::cout << "\n " << str << " has been succesfully removed.\n"; } else { std::cout << "\n No name has been removed"; if ( str != "" ) std::cout << ", " << str << " wasn't found.\n"; else std::cout << ".\n"; } break; case opt::menu: menu_options.show(); break; default: std::cout << "\n Sorry, that's not an option.\n"; } } while ( choise != opt::exit ); return 0; } 中执行。

<form id="loginForm" name="loginForm" ng-submit="submitForm()">
  <div class="form-group">
    <input type="text" class="form-control" id="userCompany" placeholder="Company" ng-model="user.companyId" required>
  </div>
  <div class="form-group">
    <input type="text" class="form-control" id="userUsername" placeholder="Username" ng-model="user.userId" required>
  </div>
  <div class="form-group">
    <input type="password" class="form-control" id="userPassword" placeholder="Password" ng-model="user.password" required>
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
  <button class="btn btn-default">Change Password</button>
  <span ng-show="errorData">{{errorData}}</span>
</form>

我希望它可以提供帮助。