首先,我想说我是新手.
我想boost:multi_array
在课堂上初始化.我知道如何创建boost:multi_array
:
boost::multi_array<int,1> foo ( boost::extents[1000] );
Run Code Online (Sandbox Code Playgroud)
但作为课程的一部分,我遇到了问题:
class Influx {
public:
Influx ( uint32_t num_elements );
boost::multi_array<int,1> foo;
private:
};
Influx::Influx ( uint32_t num_elements ) {
foo = boost::multi_array<int,1> ( boost::extents[ num_elements ] );
}
Run Code Online (Sandbox Code Playgroud)
我的程序通过编译,但在运行期间,当我尝试从foo
(例如foo[0]
)控制元素时,我收到错误.
如何解决这个问题呢?
我目前正在阅读"编程:使用C++的原理和实践",在第4章中有一个练习,其中:
我需要使用Sieve of Eratosthenes算法制作一个程序来计算1到100之间的素数.
这是我提出的计划:
#include <vector>
#include <iostream>
using namespace std;
//finds prime numbers using Sieve of Eratosthenes algorithm
vector<int> calc_primes(const int max);
int main()
{
const int max = 100;
vector<int> primes = calc_primes(max);
for(int i = 0; i < primes.size(); i++)
{
if(primes[i] != 0)
cout<<primes[i]<<endl;
}
return 0;
}
vector<int> calc_primes(const int max)
{
vector<int> primes;
for(int i = 2; i < max; i++)
{
primes.push_back(i);
}
for(int i = 0; i …
Run Code Online (Sandbox Code Playgroud) 我想弄清楚这里有几件事:
如何为下面的类实现迭代器?
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
class Node {
public:
Node(int i=0):val(i) {}
Node*& operator++(int i=0) {return next;};
T val;
Node *next;
};
//================================================
int main() {
Node<int> *head, *tmp1, *tmp2;
tmp1 = new Node<int>(0);
head = tmp1;
for (int i=1; i<10; ++i) {
tmp2 = new Node<int>(i);
tmp1->next = tmp2;
tmp1 = tmp2;
}
while (head != NULL) {
cout << head->val << " '";
head = head->operator++(0); //How do I make …
Run Code Online (Sandbox Code Playgroud)在本书中C++ Primer 13.5.1
,它使用Use-Count类实现了一个智能指针类.它们的实施如下:
使用 - 计数类
// private class for use by HasPtr only
class U_Ptr {
friend class HasPtr;
int *ip;
size_t use;
U_Ptr(int *p): ip(p), use(1) { }
~U_Ptr() { delete ip; }
};
Run Code Online (Sandbox Code Playgroud)智能指针类
/*
smart pointer class: takes ownership of the dynamically allocated
object to which it is bound
User code must dynamically allocate an object to initialize a HasPtr
and must not delete that object; the HasPtr class …
Run Code Online (Sandbox Code Playgroud)我在C++中使用Gauss-Jordan消除来求解线性方程组.代码工作正常.想知道为什么1,2,3 void gauss()
号线不能被4号线取代(这样做后输出不正确)?
#include <iostream>
using namespace std;
class Gauss
{
float a[50][50];
int n;
public:
void accept()
{
cout<<"Enter no. of variables: ";
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n+1;j++)
{
if(j==n)
cout<<"Constant no."<<i+1<<" = ";
else
cout<<"a["<<i+1<<"]["<<j+1<<"] = ";
cin>>a[i][j];
}
}
}
void display()
{
for(int i=0;i<n;i++)
{
cout<<"\n";
for(int j=0;j<n+1;j++)
{
if(j==n)
cout<<" ";
cout<<a[i][j]<<"\t";
}
}
}
void gauss()//converting augmented matrix to row echelon form
{
float temp;//Line 1
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{ …
Run Code Online (Sandbox Code Playgroud) 我已经定义了一些函数,我打印他们的地址是这样的:
#include<iostream>
#include <string>
using std::cout;
std::string func()
{
return "hello world\n";
}
int func2(int n)
{
if (n==0)
{
cout << func2 << std::endl;
return 1;
}
cout << func2 << std::endl;
return n + func2(n - 1);
}
//================================================
int main()
{
int (*fun)(int) = func2;
cout << fun;
cout << std::endl << func2(3);
}
Run Code Online (Sandbox Code Playgroud)
当我打印函数的名称(地址)时,它们都1
在我的编译器上打印(Mingw gcc 4.8).
它可以或它应该有所不同吗?
以下是来自spoj的问题的实现: - http://www.spoj.com/problems/COINS/
#include <stdio.h>
#define ll long long
ll arr[100000];
ll max(ll n)
{
if(n < 49999)// Doubt
{
if(!arr[n])
return arr[n] = max(n/2) + max(n/3) + max(n/4);
else
return arr[n];
}
else
return max(n/2) + max(n/4) + max(n/3);
}
int main()
{
ll n, c = 0, i;
for(i = 0; i < 12; i++) // Also why 12 when the input can be <12
{
arr[i] = i;
}
while(scanf("%lld", &n) != EOF)
{
printf("%lld\n", max(n));
} …
Run Code Online (Sandbox Code Playgroud) 我正在学习指针:
int x[10];
int *p = &x
Run Code Online (Sandbox Code Playgroud)
这将使指针类型int
成为第一个元素.所以,如果我有2D数组,我需要使用双指针:第一个指针指向数组的第二个维度.这意味着 :
int x[3][4] = {{1,2,3,4},{5,6,7,8},{9,9,9,9}};
Run Code Online (Sandbox Code Playgroud)
当我想指出它时,我必须像这样声明第二维的大小,对吧?
int *p[4] = x;
Run Code Online (Sandbox Code Playgroud)
或者存在通过键入另一种方式:int **p
; ?
和int *p[4]
是整数指针这需要的阵列4 * (sizeof(int*))
,是吗?
所以我试图从标准输入(使用cin
)读取这样的输入:
亚当英语85
查理数学76
埃里卡历史82
理查德科学90
我的目标是最终将每个数据块存储在我自己创建的数据结构中,因此基本上我想解析输入,因此每个数据都是单独的.由于每行输入由用户一次输入一次,因此每次我得到需要解析的整行输入.目前我正在尝试这样的事情:
stringstream ss;
getline(cin, ss);
string name;
string course;
string grade;
ss >> name >> course >> grade;
Run Code Online (Sandbox Code Playgroud)
我遇到的错误是XCode告诉我没有匹配的函数调用getline
令我感到困惑.我已经包含了string
库,所以我猜这个错误与使用getline
读取cin
到一个stringstream
?任何帮助在这里将不胜感激.
所以我必须发送到std::cout
一列数据,我必须在数据周围显示一些字符,如:
Plot Points
(0,0), (2,3)
(1,10), (12,14)
Run Code Online (Sandbox Code Playgroud)
我必须在列标题中的字母"s"
输入下右对齐最后右括号"Points"
.
我把数据输出如下:
cout << right << setw(12) << "(" << x1 << "," << y1 << "), (" << x2 << "," << y2 << ")";
Run Code Online (Sandbox Code Playgroud)
但是我看到的所有例子似乎都表明正确而且setw
似乎只影响我发送给的下一条数据cout
,因此在这种情况下"("
只有.
有没有办法将所有这些字符和变量组合在一起,以便在输出列中将它们全部合理化?
我只是在学习C++,所以期待有一些我还没有学到的简单解决方案吗?