这是一个例子:
#include <iostream>
using namespace std;
int main()
{
cout << "Hola, moondo.\n";
}
Run Code Online (Sandbox Code Playgroud)
它抛出错误:
gcc -c main.cpp gcc -o edit main.o main.o: In function `main':
main.cpp:(.text+0xa): undefined reference to `std::cout'
main.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char,std::char_traits<char> >& std::operator<< <std::char_traits<char>>(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.o: In function `__static_initialization_and_destruction_0(int,int)':
main.cpp:(.text+0x3d): undefined reference to `std::ios_base::Init::Init()'
main.cpp:(.text+0x4c): undefined reference to `std::ios_base::Init::~Init()' collect2: error: ld
returned 1 exit status make: *** [qs] Error 1
Run Code Online (Sandbox Code Playgroud)
此外,这个例子:
#include <iostream>
int main()
{
std::cout<<"Hola, moondo.\n";
}
Run Code Online (Sandbox Code Playgroud)
抛出错误:
gcc …Run Code Online (Sandbox Code Playgroud) 在课堂里:
class foo
{
public:
static int bar; //declaration of static data member
};
int foo::bar = 0; //definition of data member
Run Code Online (Sandbox Code Playgroud)
我们必须明确定义静态变量,否则会产生一个
undefined reference to 'foo::bar'
我的问题是:
请注意,这与先前提出的问题不重复undefined reference to static variable.这个问题打算问一下静态变量的明确定义背后的原因.
最近,我遇到了一个代码片段:
int i = 1;
while (N > 1)
{
N = N / i;
i = i + 1;
}
Run Code Online (Sandbox Code Playgroud)
在观察时,很明显i线性地增加,并且在循环的每个运行时间中i分开N,因此我们可以说它N作为因子的反函数的函数而减小.
我们如何用theta表示法表示这一点 ,因为没有为每个自然数定义反因子N?我们是否必须使用此函数的近似上限和下限?
计数排序是平均时间复杂度为O(n+K)的排序算法,计数排序假设每个输入元素都是0到K范围内的整数。
为什么我们不能线性搜索未排序数组中的最大值,使其等于 K,然后对其应用计数排序?
在 C++ 中,我知道创建多行字符串的最佳规范方法是创建相邻的字符串并让编译器在编译时连接它们,如下所示:
string s = "This is a very long string ...\n"
" and it keeps on going...";
Run Code Online (Sandbox Code Playgroud)
在 Java 中,我知道这样做的唯一方法是使用连接:
String s = "This is a very long string ...\n" +
" and it keeps on going...";
Run Code Online (Sandbox Code Playgroud)
问题是,这是否在运行时生成单个字符串,或者 java 实际上也在编译时连接?问题出现的原因是由于以下行为:
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2); // this prints true, because the compiler
// generates only one "abc" object
String s3 = "a";
s3 += "bc";
System.out.println(s1 == s3); // false
Run Code Online (Sandbox Code Playgroud) 所以,我目前正在阅读一些c ++的东西,我在cppreference上看到了这个例子,我无法理解这个转换是如何工作的.
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> v{1, 2, 2, 2, 3, 3, 2, 2, 1};
std::vector<int>::iterator last;
last = std::unique(v.begin(), v.end()); // 1 2 3 2 1 3 2 2 1
// ^
for (std::vector<int>::iterator it = v.begin(); it != last; ++it) {
std::cout << *it << " ";
}
std::cout << "\n";
}
Run Code Online (Sandbox Code Playgroud)
我明白,当我们用它独特的东西转移了,但我不知道我们是如何获得给我们的顺序last来v.end().
通过我自己的纸质图纸我知道我们如何从实现的顺序v.begin(),以v.last()从,但不是序列v.last()来v.end()提到.
这是参考站点.
假设我有一些价值:
double x;
Run Code Online (Sandbox Code Playgroud)
我想将它限制在某个范围内[a, b],使得结果值在该范围内:
double confine(double x, double a, double b)
{
if (x < a) return a;
else if (x > b) return b;
return x;
}
Run Code Online (Sandbox Code Playgroud)
是否有单个 boost或STL功能可以为我做这个?
我试图得到一个c ++程序总和用户输入的元素:
#include <iostream>
int main(){
int sum = 0, value = 0;
// read until end-of-file, calculating a running total of all values read
while (std::cin >> value){
sum += value;
}
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我在c ++ primer上读到了这个例子,但是当我编译这个程序并运行它时,提示会一直等待输入.为什么不输出任何东西?
我注意到在Windows上,文件打开不区分大小写.
(例如,fstream("text.txt")无论打开的实际文件名存在的:Text.txt)
我如何才能使这个区分大小写呢?(除非文件名也匹配正确的情况,否则文件无法打开)
我是C++的新手,并且对我的程序有一些帮助来比较两个XML文件.这是我的代码:
#include "pugixml.hpp"
#include <iostream>
#include <unordered_map>
int main() {
pugi::xml_document doca, docb;
std::map<string, pugi::xml_node> mapa, mapb;
if (!doca.load_file("a.xml") || !docb.load_file("b.xml"))
return 1;
for (auto& node: doca.child("site_entries").children("entry")) {
const char* id = node.child_value("id");
mapa[new std::string(id, strlen(id))] = node;
}
for (auto& node: docb.child("site_entries").children("entry"))
const char* idcs = node.child_value("id");
std::string id = new std::string(idcs, strlen(idcs));
if (!mapa.erase(id)) {
mapb[id] = node;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,我似乎遇到了很多错误.
我得到的第一个是:
src/main.cpp:10:14: error: unknown type name 'string'; did you mean 'std::string'?
std::map<string, pugi::xml_node> mapa, mapb;
~~~~~^~~ …Run Code Online (Sandbox Code Playgroud)