我不明白为什么我收到这条警告信息.
> fixed <- data.frame("Type" = character(3), "Amount" = numeric(3))
> fixed[1, ] <- c("lunch", 100)
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = "lunch") :
invalid factor level, NA generated
> fixed
Type Amount
1 <NA> 100
2 0
3 0
Run Code Online (Sandbox Code Playgroud) 尝试从向量初始化字符串.我应该把"嘿"作为输出.但我得到了"分段错误".我做错了什么?
//write a program that initializes a string from a vector<char>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main ()
{
vector<char> cvec;
cvec[0]='h';
cvec[1]='e';
cvec[2]='y';
string s(cvec.begin(),cvec.end());
cout<<s<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我收到错误消息的参数2从没有已知的转换long unsigned int来long unsigned int&当我尝试编译下面的代码:
void build(int* &array, unsigned long& index) {
if (index == 0)
return;
else {
heapify(array, index);
build(array, index-1);
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么会发生这种情况,这个错误背后的逻辑是什么?
我的程序很简单.一类银行账户.每个帐户都有余额和权力.每个账户的利率相同.我在编译时遇到错误.怎么了?提前致谢.
2 #include <iostream>
3 #include <string>
4 using namespace std;
5 class bank
6 {
7 private:
8 double balance;
9 string owner;
10 static double InterestRate;
11 public:
12 static void AccountInfo(const bank& ac)
13 {
14 cout << "name: " << ac.owner << endl << "balance: " << ac.balance;
15 }
16 static void SetAccount(bank& ac)
17 {
18 cout << "enter a name: " << flush;
19 cin >> ac.owner;
20 cout << "enter a balance: …Run Code Online (Sandbox Code Playgroud) 我的问题是const string* p给我一个错误.这有什么问题?因为我没有改变原来的价值.const int& n = 0工作良好.
#include <iostream>
using namespace std;
class HasPtr
{
private:
int num;
string* sp;
public:
//constructor
HasPtr(const int& n = 0, const string* p = 0): num(n), sp(p) {}
//copy-constructor
HasPtr(const HasPtr& m): num(m.num), sp(m.sp) {}
//assignment operator
HasPtr& operator=(const HasPtr& m)
{
num = m.num;
sp = m.sp;
return *this;
}
//destructor
~HasPtr() {}
};
int main ()
{
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出错误是:
error: invalid conversion from ‘const std::string*’ …Run Code Online (Sandbox Code Playgroud) 如果我的代码中没有 istring.clear.() ,则输出将为“nan%”。一切正常,输出为 60%(如果有的话)。它在那里到底有什么作用?为什么它会有所不同?(ps我的输入是“ynyn y”)
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
//inline function
inline ifstream& read(ifstream& is, string f_name);
//main function
int main ()
{
string f_name=("dcl");
ifstream readfile;
read(readfile, f_name);
string temp, word;
istringstream istring;
double counter=0.0, total=0.0;
while(getline(readfile,temp))
{
istring.str(temp);
while(istring>>word)
{
if(word=="y")
++counter;
if(word=="n" || word=="y")
++total;
}
istring.clear();
}
double factor=counter/total*100;
cout<<factor<<"%"<<endl;
return 0;
}
inline ifstream& read(ifstream& is, string f_name)
{
is.close();
is.clear();
is.open(f_name.c_str());
return is;
}
Run Code Online (Sandbox Code Playgroud) 它看起来像我cout*cp,它只输出字符串的第一个字母,在我把它们放在矢量后,我的输出是空白的.我究竟做错了什么?
//write a program to assign the elements from a list of char* pointers to c-style character strings to a vector of strings
#include <iostream>
#include <cstring>
#include <vector>
#include <list>
#include <string>
using namespace std;
int main ()
{
list<const char*> clist;
cout<<"please enter a string"<<endl;
for(string s; getline(cin,s); )
{
const char* cp=s.c_str();
clist.push_back(cp);
cout<<*cp;
}
cout<<*clist.begin();
vector<string> svec;
svec.assign(clist.begin(),clist.end());
for(vector<string>::iterator iter=svec.begin(); iter!=svec.end(); ++iter)
cout<<*iter<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想在for-init声明中声明两种类型的变量.像下面的代码.我知道"for(string word,int numb,; cin >> word >> numb;)"不起作用.只是想让你知道我想要做什么.我的目标是声明具有相同生命周期的两种类型的变量并将它们组合在一起.其他编码方式也很有用.提前致谢.
#include <iostream>
#include <vector>
#include <string>
#include <utility>
using namespace std;
int main ()
{
cout<<"enter a word and a number"<<endl;
for (string word, int numb, ; cin>>word>>numb; )
{
//do some work
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
好吧,我认为这是我能得到的最接近的建议.
#include <iostream>
#include <vector>
#include <string>
#include <utility>
using namespace std;
int main ()
{
vector<pair<string,int> > pvec;
cout<<"enter a word and a number"<<endl;
{
int numb=0;
for (string word; cin>>word>>numb; )
pvec.push_back(make_pair(word,numb));
} …Run Code Online (Sandbox Code Playgroud) 这是一个赋值运算符.&rhs != this令人困惑.我的问题:rhs是Message类型的引用.什么&rhs意思?做&什么(引用的内存地址?)?另一个问题是return *this.因为我们想要一个类型为Message的引用,但是*这是一个Message类型的对象,对吧?我们如何将对象返回引用?
Message& Message::operator=(const Message &rhs)
{
if (&rhs != this)
{
some functions;
}
return *this;
}
Run Code Online (Sandbox Code Playgroud) 我只是想测试一下.我想知道我做错了什么?
#include <iostream>
using namespace std;
unsigned long pwr(unsigned long n, unsigned long m)
{
if(m == 0)
n = 1;
if(m == 1)
n = n;
n = pwr(n, m/2) * pwr(n, m/2);
return n;
}
int main ()
{
unsigned long n(2), m(16);
cout << pwr(n, m);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是
Segmentation fault
Run Code Online (Sandbox Code Playgroud) 我正在书中提出一个问题,要求我为具有以下变量的类编写构造函数和复制控制成员.有人可以给出这个类有用的场景/示例吗?主要功能可能有些代码吗?我感到困惑的部分是为什么它需要TreeNode*left和TreeNode*.我想不出它们的使用方法.
#include <iostream>
#include <string>
using namespace std;
class TreeNode
{
public:
//constructor
TreeNode(const string& s, const int& n, const TreeNode& lm, const TreeNode& rm):
value(s), count(n), left(new TreeNode(lm)), right(new TreeNode(rm)) {}
//copy-constructor
TreeNode(const TreeNode& m): value(m.value), count(m.count), left(new TreeNode(*m.left)), right(new TreeNode(*m.right)) {}
//assignment operator
TreeNode& operator=(const TreeNode& m)
{
value = m.value;
count = m.count;
*left = *m.left;
*right = *m.right;
return *this;
}
//destructor
~TreeNode()
{
delete left;
delete right;
}
private:
string value;
int count;
TreeNode *left; …Run Code Online (Sandbox Code Playgroud) 我的问题是我有两个论点vector<int>& ivec和const int& numb.有没有办法消除const int& numb?我只想对向量中的所有元素添加10.我也想消除if部分,因为它是空的.提前致谢.请递归地做.我的目标是在递归函数中使用尽可能少的参数.
#include <iostream>
#include <vector>
using namespace std;
vector<int> addten(vector<int>& ivec, const int& numb) {
if (numb == 0) {
} else {
ivec[numb - 1] += 10;
addten(ivec, numb - 1);
}
return ivec;
}
Run Code Online (Sandbox Code Playgroud)