c++中的向量与数组
vector vs. arrays in c++
我的方法有问题。我希望它接受字符串数组作为第一个参数,而不是向量字符串。然而,当我尝试使用字符串数组并在主函数中创建一个字符串数组时,我会遇到各种错误。我不知道是应该为我的参数使用一个指向字符串数组的指针,还是只使用一个字符串。有什么帮助吗?
#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;
}
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);
}
使用如下:
std::string Database[] ={ "Justin", "Justin1", "Justin2","Justin3" };
newmember
作为
string newMember(std::string exist[], std::size_t n, string newname)
用n
代替exist.size()
在main
:中
cout << u.newMember(Database, 4,"Justin") << endl;
同样根据编辑后
运算符=
与运算符==
不同,第一个是赋值运算符(将右边的值赋值给左边的变量),另一个==
是相等运算符
所以你需要使用作为:
Database[0] = "Justin";
Database[1] = "Justin1";
Database[2] = "Justin2";
Database[3] = "Justin3";
相关文章:
- 将数组/向量发送到 c++ 脚本的 cython 示例
- C++:在多个线程中访问同一数组/向量的不同单元格是否会产生数据竞赛?
- 将指向多维数组向量中的单个项的指针传递给函数 c++
- 数组向量的文件 I/O
- 在一行中初始化和引入数组向量的正确方法?
- C++ 返回具有最小硬币的数组/向量以获得价值动态规划
- 将整数向量转换为字节数组向量,然后调用每个字节数组
- 我可以在不循环访问数组/向量的情况下检查数组/向量中的单词吗?
- (C )创建可以从功能访问的动态全局数组/向量
- 提升是否具有用于数组向量的智能指针
- 将函数应用于数组向量的元素
- 数组向量的内存布局是什么
- 获取数组/向量末尾地址的未定义行为
- "Empty"数组\向量成员 C++
- 将字符串向量转换为双2-D数组向量
- 排序多维数组/向量
- 如何创建模板化对象的数组/向量
- 如何在数组向量上使用 std::find
- 无法删除C++中的数组/向量
- 具有元素自定义构造函数的对象数组/向量