[C++関数を書いて10進数を2から9に変換する任意の進数練習リストコンテナ]
9896 ワード
1.listにoperator<2を再ロードし、listオブジェクトlstを定義し、要素値は{0,1,2,3,4,5,6,7,8,9}で、結果を表示します.3、push_backは5つで、結果を表示し、removeアルゴリズムを呼び出してすべての5を削除し、結果を表示します.4、reverseアルゴリズムを呼び出してlstを反転し、結果を表示する.5、lstを昇順に並べ、結果を表示します.6、dequeオブジェクトdを定義し、lstを使用して初期化し、結果を表示する.push_front 1つ7、priorityを定義するqueueは、d初期化を使用し、pushは5つです.top、pop 8を出力し、queueオブジェクトを定義し、queueの基本操作を検証します.size、empty、push、pop、front、backなど.9、1つの関数string DecToOther(unsigned int dec,unsigned int n)を書き、10進数を2から9の任意の進数に変換し、関数は変換結果を返し、decは10進数、nは2~9の1つである.テストプログラムを作成します.
#include
#include
#include
#include
#include
using namespace std;
// list operator<
ostream& operator<list<int> ::iterator& it)
{
os << *it << "\t";
return os;
}
// string DecToOther( unsigned int dec , unsigned int n), 2 9 , ,dec ,n 2~9 。 。
void dectoother(unsigned int dec, unsigned int n_num)
{
int effect = 0;
int i = dec;
vector<int> q_dec;
while (i != 0)
{
effect = i % n_num;
if (i >= n_num)
i = i / n_num;
else
{
q_dec.push_back(effect);
break;
}
// cout << j << endl;
q_dec.push_back(effect);
}
int nSize = q_dec.size();
cout << dec << " " << n_num << " " << " : ";
for (int i = nSize - 1; i >= 0; --i)
{
int &nValue = q_dec[i];
cout << nValue;
}
cout << endl;
}
int main()
{
// list lst, {0,1,2,3,4,5,6,7,8,9}, 。
cout << " list lst, {0,1,2,3,4,5,6,7,8,9}, " << endl << endl;
list<int>lst;
int i = 0;
for (i = 0; i < 10 ; ++i)
{
lst.push_back(i);
}
list<int>::iterator ite;
for (ite = lst.begin(); ite != lst.end(); ++ite)
{
cout << *ite << "\t";
}
//push_back 5, , remove 5, 。
cout << "push_back 5, , remove 5, " << endl << endl;
lst.push_back(5);
for (ite = lst.begin(); ite != lst.end(); ++ite)
{
cout << *ite << "\t";
}
cout << endl;
lst.remove(5);
for (ite = lst.begin(); ite != lst.end(); ++ite)
{
cout << *ite << "\t";
}
cout << endl;
// reverse lst, 。
cout << " reverse lst, " << endl << endl;
list<int>::reverse_iterator rite = lst.rbegin();
for (; rite != lst.rend(); ++rite)
{
cout << *rite << "\t";
}
cout << endl;
//5 lst , 。
cout << " lst , " << endl << endl;
lst.sort();
for (ite = lst.begin(); ite != lst.end(); ++ite)
{
cout << *ite << "\t";
}
cout << endl;
//6 deque d, lst , 。push_front -1, 。
cout << " deque d, lst , 。push_front -1, " << endl << endl;
deque<int>d(lst.begin(), lst.end());
deque<int>::iterator it = d.begin();
for (; it != d.end(); ++it)
{
cout << *it << "\t";
}
cout << endl;
d.push_front(-1);
for (i = 0; icout << d.at(i) << "\t";
}
cout<//7 priority_queue, d ,push 5。 top、pop 5 。
cout << " priority_queue, d ,push 5。 top、pop 5 " << endl << endl;
priority_queue<int>pr(d.begin(), d.end());
pr.push(5);
for (i = 1; i <= 5; ++i)
{
cout << pr.top() << "\t";
pr.pop();
}
cout << endl;
//8 queue , queue 。size、empty、push、pop、front、back 。
cout << " queue , queue 。size、empty、push、pop、front、back " << endl << endl;
queue<int>q;
for (i = 1; i <= 10; ++i)
{
q.push(i);
}
cout << q.size() << "\t" << q.front() << "\t" << q.back() << "\t";
while (!q.empty())
{
for (i = 0; icout << endl << endl;
dectoother(2, 2);
dectoother(3, 2);
dectoother(4, 2);
dectoother(5, 2);
dectoother(16, 8);
dectoother(14, 3);
system("pause");
return 0;
}