1

I am having trouble with my method. I want it to accept an array of strings as its first argument instead of a vector string. However when I try to use an Array of strings and make one in the main function I get all kinds of errors. I don't know if I should user a pointer to an array of strings for my argument or just a string. Any help?

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <sstream>
#include<iostream> 
using namespace std;
class UserName
{
    public:
    string newMember(string* exist, string newname) { 
    bool found = false;
    bool match = false;
    stringstream ss;
    string result;
    string othername;
    for(int i = 0; i < exist.size(); i++){
        if(exist[i] == newname){
            found = true;
        break;
        }
    }
    if(found){
        for(int x = 1;   ; x++){
            match = false;
        ss.str("");
        ss << newname << x;
        for(int i = 0; i < exist.size();i++){
            //cout << ss.str() << endl;
            othername = ss.str();
            if(exist[i] == othername){
                match = true;
            break;
            }
        }
        if(!match){
            result = ss.str();
            break;
        }
        }
        return result;
    }
    else return newname;
    }   
};
int main(){
    UserName u;
    string Database [4];
    Database[0] == "Justin";
    Database[1] == "Justin1";
    Database[2] == "Justin2";
    Database[3] == "Justin3";
    cout << u.newMember(Database, "Justin") << endl;
    return 0;
}
4

2 に答える 2

6

残念ながら、C++ の配列は特殊なケースであり、多くの点で適切な値のように動作しません。いくつかの例:

void foo(int c[10]); // looks like we're taking an array by value.
// Wrong, the parameter type is 'adjusted' to be int*

int bar[3] = {1,2};
foo(bar); // compile error due to wrong types (int[3] vs. int[10])?
// No, compiles fine but you'll probably get undefined behavior at runtime

// if you want type checking, you can pass arrays by reference (or just use std::array):
void foo2(int (&c)[10]); // paramater type isn't 'adjusted'
foo2(bar); // compiler error, cannot convert int[3] to int (&)[10]

int baz()[10]; // returning an array by value?
// No, return types are prohibited from being an array.

int g[2] = {1,2};
int h[2] = g; // initializing the array? No, initializing an array requires {} syntax
h = g; // copying an array? No, assigning to arrays is prohibited

ここから撮影)

適切な値のように動作する配列が必要な場合は、 を使用しますstd::array

#include <array>
#include <string>

void foo(std::array<std::string, 10> arr) { /* ... */ }

int main() {
  std::array<std::string, 10> arr = {"Justin", "Justin1", "Justin2", "Justin3"};
  foo(arr);
}
于 2013-08-18T14:15:01.613 に答える
4

次のように使用します。

std::string Database[] ={ "Justin", "Justin1", "Justin2","Justin3" };

newmemberなので

string newMember(std::string exist[], std::size_t n, string newname)

exist.size()と置き換えますn

main

cout << u.newMember(Database, 4,"Justin") << endl;

また、編集した投稿に従って

operator=は operator と同じではありません。==最初の演算子は代入演算子 (右側の値を左側の変数に代入する) で、もう 1 つ==は等価演算子です。

したがって、次のように使用する必要があります。

Database[0] = "Justin";
Database[1] = "Justin1";
Database[2] = "Justin2";
Database[3] = "Justin3";
于 2013-08-18T14:15:53.380 に答える