嗨,我想制作一个外部单链表.我有一个问题"赋值中的非Ivalue"及其出现在行"this = currP-> next"我试图使它成为currP.next但它也产生错误
#include <cstdlib>
using namespace std;
struct node{
int data;
node *next;
node(int i){
data = i;
next = NULL;
}
void insert(int position, node &n){
node *currP = this;
node *prevP= NULL;
for(int counter = 0; counter>=position;counter++, prevP = currP, currP = currP->next){
if(counter==position)
{
n.next = currP->next;
currP->next = &n;
}
}
}
void add(node &n){
next = &n;
}
void deleteNode(int i){
node *currP = this;
node *prevP = NULL;
while(currP!= NULL){
if(currP->data == …Run Code Online (Sandbox Code Playgroud) 我想在for循环中声明几个变量:
for ( int x = 0, int y = 0 ; ; )
{
}
Run Code Online (Sandbox Code Playgroud)
,但这不起作用,因为我不能在逗号后面指定一个类型,.在这种情况下,删除第二个int或y在循环外声明将解决问题,但如果我想在循环内声明两个变量并且也有不同的类型怎么办?
我可以有类似的东西:
for ( int x = 0, float y = 0 ; ; )
{
}
Run Code Online (Sandbox Code Playgroud)
?
我们已经开始在学校学习Java,我们已经做了一些功课.我已经成功完成了5分中的4分,但最后一分是真正的痛苦.
基本上:编写一个程序,找到(在1000个长数字中)五个连续数字的最大乘积.
这是http://pastebin.com/PFgL6jcM的数字
你有任何想法如何解决这个问题?
如果这是不明确的指示,请通知我,并会再次向您解释.
假设我有一个具有以下定义的Employee类:
class Employee {
private final String id;
private final String name;
private final String dept;
private final Address address;
public Employee(String id, String name, String dept, Address address) {
this.id = id;
this.name = name;
this.dept = dept;
this.address = address;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public String getDept() {
return dept;
}
public Address getAddress() {
return address;
}
}
class Address {
private String addrLine1;
private String …Run Code Online (Sandbox Code Playgroud) 我试过这个:
class cls1{
public:
cls1(){ cout << "cls1 constructor\n";}
cls1 (cls1 & o){ cout << "cls1 copy constructor\n";}
};
class cls2{
public:
cls2 () { cout << "cls2 constructor\n";}
cls2 (cls2 & o){ cout << "cls2 copy constructor\n";}
};
class my_cls{
public:
cls1 o1;
cls2 o2;
my_cls(){ cout << "my_cls constructor\n";}
my_cls(my_cls& o){ cout << "my_cls copy constructor\n";}
};
void f(my_cls o){}
int main(){
my_cls p;
f(p);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但输出是:
cls1 constructor
cls2 constructor
my_cls constructor
cls1 constructor
cls2 …Run Code Online (Sandbox Code Playgroud) 我是c ++的新手,我正在努力了解它是如何工作的.所以我有下一个代码:
int _tmain(int argc, _TCHAR* argv[])
{
std::wcin.get();//for console window input
int i=1;
// decode arguments
if(argc < 2) {
printf("You must provide at least one argument\n");
exit(0);
}
// report settings
for (;i<argc;i++) printf("Argument %d:%s\n",i,argv[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
了解argc和argv是如何工作的.但是,无论我在控制台中输入多少个参数,argc都不会增加.它仍然是1.我知道argv [0]是程序名.我的输入类似于:"aaa bbb ccc ddd".我相信这个argc应该是5对吗?我究竟做错了什么 ?为什么它不会增加?
谢谢 .
首先,我想说这是一个硬件任务,我只是对我面临的错误有疑问
我创建了一个带有插入函数的矢量模板,该函数将data_type添加到动态数组的末尾.这就是我目前所拥有的.
// Insert the value at the specified index by moving the tail
// when Size = Capacity the Vector has to be reallocated first
// to accomate the new element
void Insert(const DATA_TYPE& value, int index){
// If the Capacity is not large enough then ...
// allocate large vector
if (Size >= Capacity){
// 0. Let's boost the capacity
Capacity += CAPACITY_BOOST;
// 1. Allocate new larger vector
DATA_TYPE* newData = new DATA_TYPE[Capacity];
// 2. Copy from …Run Code Online (Sandbox Code Playgroud) 我有以下功能:
int v[]={0,1,1,1};
int f(int x)
{
if(x>=1) return v[x]+f(x-1);
else return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在主要方面打电话时它是这样的:cout<<f(4);它输出'4'但我希望是'3'有人可以澄清为什么我得到'4'而不是'3'?(我想我错过了什么)
示例代码:
MyItemType a;
MyItemType b;
a.someNumber = 5;
b = a;
cout << a.someNumber << endl;
cout << b.someNumber << endl;
b.someNumber = 10;
cout << a.someNumber << endl;
cout << b.someNumber << endl;
Run Code Online (Sandbox Code Playgroud)
输出:
5
5
5
10
Run Code Online (Sandbox Code Playgroud)
如果a和b是引用类型,那么最后2行将是10和10而不是5和10我猜.
当你做这样的声明时,这是否意味着:
AClassType anInstance;
Run Code Online (Sandbox Code Playgroud)
它被视为一种价值类型?
------这是MyItemType.h ------------
#ifndef MYITEMTYPE_H
#define MYITEMTYPE_H
class MyItemType{
public:
int someNumber;
MyItemType();
};
MyItemType::MyItemType(){
}
#endif /* MYITEMTYPE_H */
Run Code Online (Sandbox Code Playgroud) 从
3)[...]输入迭代器的算法绝不应该尝试两次通过相同的迭代器.它们应该是单通道算法.[...]
这个IMO限制了一些相当直接的优化(例如通过容器一次以查看它有多少元素) - 唉,动机超出了问题的范围.
为什么这个要求?
c++ ×8
java ×2
algorithm ×1
command-line ×1
constructor ×1
for-loop ×1
iterator ×1
oop ×1
ostream ×1
pointers ×1
value-type ×1
vector ×1