- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我下面的程序读取一个文本文件 (data.txt),使用 upper_bound() 来比较一个值,但它没有给出正确的值。我不明白为什么输出总是最后一个值。
#include <iostream>
#include <vector>
#include <algorithm>
int main(int argc, char* argv[])
{
FILE* textFile;
std::string filename;
std::vector <unsigned> dataVector;
unsigned d;
// Open file
filename = "data.txt";
if( (textFile = fopen(filename.c_str(), "r")) == NULL ) {
std::cout << "Cannot open file "<< filename.c_str() << std::endl;
exit(1);
}
// Read data
while ( fscanf(textFile, "%d", &d) > 0 ){
dataVector.push_back(d);
}
fseek(textFile, 1, SEEK_CUR);
std::cout<<"dataVector size: "<< dataVector.size() << std::endl;
// ********************* iterator ******************************** //
unsigned val;
unsigned x;
std::vector<unsigned>::iterator it;
std::vector<unsigned>::iterator begin;
std::vector<unsigned>::iterator end;
val = 41;
begin = dataVector.begin();
end = dataVector.end();
it = upper_bound (begin, end, val); // Problem here: it = end
x = int(it - begin); // output: 424 is wrong, supposed to be 64!!
return 0;
}
data.txt(这个不行)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 8 14 22 32 44 58 75 93 113 134 157 183 211 240 271 304 339 376 415 455 497 539 581 623 665 708 752 797 843 890 938 988 1040 1093 1148 1204 1262 1321 1381 1442 1504 1568 1634 1701 1770 1840 1911 1983 2056 2132 2209 2287 2365 2433 2498 2558 2611 2658 2705 2753 2801 2849 2898 2947 2996 3046 3095 3145 3195 3246 3296 3348 3399 3451 3504 3556 3607 3658 3708 3758 3808 3859 3911 3960 4011 4060 4106 4150 4194 4237 4277 4319 4362 4403 4441 4480 4520 4557 4595 4629 4657 4687 4715 4742 4769 4795 4819 4844 4867 4888 4906 4921 4936 4950 4958 4963 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
data_2.txt(有效)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 5 14 31 51 73 102 141 182 225 279 335 393 453 516 582 651 724 801 880 967 1054 1140 1228 1318 1409 1501 1597 1693 1791 1890 2001 2117 2251 2393 2535 2680 2827 2973 3121 3272 3425 3581 3740 3896 4054 4212 4371 4528 4688 4850 5011 5176 5341 5504 5662 5813 5961 6111 6264 6416 6569 6723 6881 7041 7200 7356 7509 7654 7796 7937 8073 8207 8340 8470 8598 8724 8850 8975 9098 9219 9339 9458 9574 9688 9802 9915 10027 10139 10251 10363 10474 10583 10693 10804 10914 11022 11129 11233 11336 11439 11541 11643 11748 11855 11965 12076 12188 12313 12440 12572 12706 12860 13024 13190 13359 13530 13702 13876 14052 14229 14429 14631 14839 15051 15268 15487 15707 15929 16155 16385 16617 16852 17091 17336 17584 17835 18090 18350 18615 18882 19154 19426 19702 19982 20264 20555 20847 21140 21436 21733 22031 22332 22634 22938 23243 23550 23858 24173 24489 24807 25128 25450 25773 26096 26419 26742 27065 27388 27711 28034 28357 28679 29001 29323 29644 29965 30286 30607 30925 31236 31544 31851 32149 32442 32725 33000 33259 33508 33732 33956 34182 34407 34632 34857 35081 35301 35507 35706 35894 36076 36254 36429 36602 36772 36940 37100 37257 37400 37532 37665 37797 37928 38059 38189 38318 38444 38571 38699 38827 38955 39083 39212 39342 39472 39602 39732 39862 39993 40124 40255 40386 40516 40646 40775 40904 41033 41163 41293 41423 41553 41682 41812 41942 42072 42202 42333 42464 42595 42726 42856 42988 43123 43259 43395 43531 43667 43803 43939 44075 44211 44346 44483 44621 44760 44900 45042 45184 45328 45472 45616 45759 45902 46045 46188 46332 46476 46621 46766 46911 47058 47205 47353 47502 47652 47803 47954 48106 48259 48413 48567 48721 48875 49029 49183 49337 49489 49641 49792 49942 50092 50243 50397 50553 50711 50869 51027 51186 51345 51504 51664 51824 51984 52144 52305 52465 52626 52787 52948 53109 53271 53433 53595 53757 53921 54085 54249 54413 54578 54742 54907 55072 55237 55402 55567 55732 55898 56065 56232 56400 56569 56738 56907 57074 57242 57410 57579 57749 57919 58088 58257 58426 58593 58759 58925 59090 59255 59420 59585 59749 59913 60076 60240 60403 60566 60728 60890 61051 61212 61373 61534 61695 0 0 0 0
最佳答案
搜索可以在一行中完成。
auto val = 41u;
auto it = std::find_if(dataVector.begin(), dataVector.end(),
[val](unsigned i) { return i > val; });
std::cout << (it - dataVector.begin()) << std::endl;
如果您重复搜索相同的范围,以至于二分搜索的复杂性降低实际上很重要,并且范围被排序直到尾随零,那么我们可以去搜索排序的子范围结束的位置(和尾随零开始),并在初始排序的子范围上使用 upper_bound
。
此外,fscanf
调用会导致未定义的行为,因为您将 %d
与 unsigned
一起使用。您需要 %u
。或者,摆脱 C 风格的困惑并使用流迭代器读入 vector :
std::ifstream ifs(filename);
if( !ifs ) {
std::cout << "Cannot open file "<< filename << std::endl;
return 1;
}
std::vector<unsigned> dataVector(std::istream_iterator<unsigned>(ifs), {});
Demo .
关于c++ - upper_bound() 给出了错误的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30795441/
#include using namespace std; class C{ private: int value; public: C(){ value = 0;
这个问题已经有答案了: What is the difference between char a[] = ?string?; and char *p = ?string?;? (8 个回答) 已关闭
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 此帖子已于 8 个月
除了调试之外,是否有任何针对 c、c++ 或 c# 的测试工具,其工作原理类似于将独立函数复制粘贴到某个文本框,然后在其他文本框中输入参数? 最佳答案 也许您会考虑单元测试。我推荐你谷歌测试和谷歌模拟
我想在第二台显示器中移动一个窗口 (HWND)。问题是我尝试了很多方法,例如将分辨率加倍或输入负值,但它永远无法将窗口放在我的第二台显示器上。 关于如何在 C/C++/c# 中执行此操作的任何线索 最
我正在寻找 C/C++/C## 中不同类型 DES 的现有实现。我的运行平台是Windows XP/Vista/7。 我正在尝试编写一个 C# 程序,它将使用 DES 算法进行加密和解密。我需要一些实
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有没有办法强制将另一个 窗口置于顶部? 不是应用程序的窗口,而是另一个已经在系统上运行的窗口。 (Windows, C/C++/C#) 最佳答案 SetWindowPos(that_window_ha
假设您可以在 C/C++ 或 Csharp 之间做出选择,并且您打算在 Windows 和 Linux 服务器上运行同一服务器的多个实例,那么构建套接字服务器应用程序的最明智选择是什么? 最佳答案 如
你们能告诉我它们之间的区别吗? 顺便问一下,有什么叫C++库或C库的吗? 最佳答案 C++ 标准库 和 C 标准库 是 C++ 和 C 标准定义的库,提供给 C++ 和 C 程序使用。那是那些词的共同
下面的测试代码,我将输出信息放在注释中。我使用的是 gcc 4.8.5 和 Centos 7.2。 #include #include class C { public:
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我的客户将使用名为 annoucement 的结构/类与客户通信。我想我会用 C++ 编写服务器。会有很多不同的类继承annoucement。我的问题是通过网络将这些类发送给客户端 我想也许我应该使用
我在 C# 中有以下函数: public Matrix ConcatDescriptors(IList> descriptors) { int cols = descriptors[0].Co
我有一个项目要编写一个函数来对某些数据执行某些操作。我可以用 C/C++ 编写代码,但我不想与雇主共享该函数的代码。相反,我只想让他有权在他自己的代码中调用该函数。是否可以?我想到了这两种方法 - 在
我使用的是编写糟糕的第 3 方 (C/C++) Api。我从托管代码(C++/CLI)中使用它。有时会出现“访问冲突错误”。这使整个应用程序崩溃。我知道我无法处理这些错误[如果指针访问非法内存位置等,
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我有一些 C 代码,将使用 P/Invoke 从 C# 调用。我正在尝试为这个 C 函数定义一个 C# 等效项。 SomeData* DoSomething(); struct SomeData {
这个问题已经有答案了: Why are these constructs using pre and post-increment undefined behavior? (14 个回答) 已关闭 6
我是一名优秀的程序员,十分优秀!