有人把这篇文章给我的注意,声称(我意译)的STL长期被误指整个C++标准库,而不是从SGI STL中采取的部分.
(...)它指的是"STL",尽管很少有人仍然使用STL(在SGI设计).
C++标准库的一部分基于STL的一部分,正是这些部分,许多人(包括几位作者和臭名昭着的错误记录的cplusplus.com)仍然称为"STL".但是,这是不准确的; 事实上,C++标准从未提及"STL",两者之间存在内容差异.
(...)"STL"很少用于指代恰好基于SGI STL的stdlib的位.人们认为这是整个标准库.它被放在简历上.这是误导.
我几乎不了解C++的历史,所以我不能判断文章的正确性.我应该避免使用术语STL吗?或者这是一个孤立的意见?
我正在编写"开始使用C++早期对象"第7版中的一个编程挑战,其中一个分配要求创建一个派生自STL字符串类的类.我发布的问题是为了理解我被允许做什么,以及我应该如何实施解决方案,以便没有人提供更高级的建议.
- 问题,因为它写在文本中 -
回文测试
Palindrome是一个向前读取相同的字符串.例如,妈妈,爸爸,女士和雷达这些词都是回文.写一个class Pstring派生自STL string class.在Pstring class增加了一个成员函数
bool isPalindrome()
Run Code Online (Sandbox Code Playgroud)
确定该字符串是否是回文.包含一个构造函数,该构造函数将STL string对象作为参数并将其传递给字符串基类构造函数.通过主要程序测试您的类,该程序要求用户输入字符串.程序使用字符串初始化Pstring对象,然后调用isPalindrome()来确定输入的字符串是否是回文.
您可能会发现使用字符串类的下标operator []很有用:如果str是一个字符串对象而k是一个整数,则str [k]返回字符串中位置k的caracter.
- 结束 -
我的主要问题是如何访问保存我的字符串对象的成员变量,如果我从派生Pstring的类是一个我没写的类,我不知道它是如何实现其成员的?
例如,
#include <string>
using namespace std;
class Pstring : public string
{
public:
Pstring(std::string text)
: string(text) { }
bool isPalindrome()
{
// How do I access the string if I am passing it to the base class?
// What I think I should do …Run Code Online (Sandbox Code Playgroud) 我遇到了一个常见的面试问题,即找到最接近的回文数.假设输入为127,则输出为131,如果为125则输出为121.
我可以提出逻辑,但我的逻辑在某些情况下失败,例如91,911.在这些输入中,它给出99,919,但正确的输出是88和909.
算法步骤是:
我一直在用C++写一个回文查找器,我已经成功地编写了一个......至少可以说是基本的.
我只想增加程序的速度,现在需要大约1m 5s才能使用我拥有的功能在1500字的单词列表上运行palindromes/2字回文测试.我想尝试在更大的文件上运行它,但却看不到我可以进一步优化的位置?
任何帮助将不胜感激:PS这不是为了学校,只是为了休闲.
#include <iostream>
#include <ostream>
#include <vector>
#include <fstream>
#include <algorithm>
using namespace std;
bool isPal(string);
int main() {
vector<string> sVec;
vector<string> sWords;
vector<string> sTwoWords1;
vector<string> sTwoWords2;
char myfile[256]="/home/Damien/Test.txt";
ifstream fin;
string str;
fin.open(myfile);
if(!fin){
cout << "fin failed";
return 0;
}
while(fin){
fin >> str;
sWords.push_back(str);
if(!fin){
break;
}
if(isPal(str)){
sVec.push_back(str);
}
else{
getline(fin, str);
}
}
reverse(sVec.begin(), sVec.end());
for(int i =0; i < sVec.size(); i++){
cout << sVec[i] << " is a Palindrome " <<endl; …Run Code Online (Sandbox Code Playgroud) 如何在 C++ 中存储大量近 100000 位数字?...
我试过使用long long int并且long double int..对我没有任何作用..
有没有其他方法可以存储这么大的数字?
我希望找到比给定的巨大数字更大的最小回文数。
我做了一个程序来查找输入的字符串是否是回文序列,但它总是说它不是回文结构
#include <conio.h>
#include <graphics.h>
#include <string.h>
void main(void)
{
int i,len,halflen,flag=1;
char str[50];
clrscr();
printf("Enter a string:\n");
gets(str);
len=strlen(str);
halflen=len/2;
for(i=0;i<halflen;i++)
{
if(str[i]!=str[i+halflen])
flag=0;
break;
}
if(flag)
printf("It is a Palindrome.");
else
printf("It is not a Palindrome.");
getch();
}
Run Code Online (Sandbox Code Playgroud)