[英]vector vs. arrays in c++
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. 但是,当我尝试使用字符串数组并在main函数中创建一个时,会遇到各种错误。 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;
}
Arrays in C++ are unfortunately a special case, and in many ways do not behave like proper values. 不幸的是,C ++中的数组是一种特殊情况,并且在许多方面都不像正确的值。 A few examples: 一些例子:
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
If you want an array that behaves like a proper value use std::array
. 如果您想要一个行为像一个适当值的数组,请使用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);
}
Use like following: 用法如下:
std::string Database[] ={ "Justin", "Justin1", "Justin2","Justin3" };
newmember
as newmember
为
string newMember(std::string exist[], std::size_t n, string newname)
replace exist.size()
with n
用n
替换exist.size()
In main
: 在main
:
cout << u.newMember(Database, 4,"Justin") << endl;
Also as per your edited post 同样根据您编辑的帖子
The operator =
is not the same as the operator ==
, the first one is an assignment operator (assigns the value at its right to the variable at its left) and the other one ==
is the equality operator 运算符=
与运算符==
,第一个运算符是赋值运算符(将其右侧的值赋给其左侧的变量),而另一个==
是相等运算符
So you need to use as: 因此,您需要用作:
Database[0] = "Justin";
Database[1] = "Justin1";
Database[2] = "Justin2";
Database[3] = "Justin3";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.