stringの文字列置換

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
std::string

stringの文字列置換

#1

投稿記事 by std::string » 9年前

Win7 32bit 、VisualStudio2013C++ C++、Win32API表示で開発しております。

code
const int Nx = 3;
const int Ny = 3;
const std::string Chars[Ny][Nx] = {
"1", "2", "3",
"4", "5", "6",
"7", "8", "9"
}
/code
と設定した文字列を、例えばキー入力一つで上記3×3の文字列を
"a", "b", "c",
"d", "e", "f",
"g", "h", "i"
と文字列置換をしたいのですが、ご教授下さい。
replace()も試みてみたのですが、理解が及ばない状態です。

アバター
h2so5
副管理人
記事: 2212
登録日時: 13年前
住所: 東京
連絡を取る:

Re: stringの文字列置換

#2

投稿記事 by h2so5 » 9年前

置換するルールを明確にしてください。 "0" や "10" はどのように置換されるのですか。

std::string

Re: stringの文字列置換

#3

投稿記事 by std::string » 9年前

"10"は今回検討していないです。

ふぁい

Re: stringの文字列置換

#4

投稿記事 by ふぁい » 9年前

質問をするのであれば、仕様や目的をもう少し詳しく説明してください。

置換ということですが、元のstring配列を破壊的に書き換えて良いのでしょうか。
もしそうでしたら、stringにconstは不要です。

キー入力一つで置換する、とはどういう意味ですか。
またstringの2次元配列を使用していますが、一般的なstring配列の用法とは違うように見えます。
char配列ではなく、本当にstringで良いのですか?
文字列の2次元配列で置換を行いたいなら、単純に2重のfor文を使用し、
if(Chars[y][x] == "1") Chars[y][x] = "a";
というような書き方をすれば良いのではないでしょうか。

std::string

Re: stringの文字列置換

#5

投稿記事 by std::string » 9年前

はい、申し訳ありませんでした。

目的としましては、簡単に言ってしまえばスマホなどで見られるキーボード画面を再現したいのです。
[ひらがな]→[英字]→[数字]→[ひらがな]と、例えばスペースキーを一度押すごとに切り替えられる機能が欲しいです。

stringは別件の機能で使用する時に都合が良いので使っています。

if文は試してみましたが、「'std::string::operator =(char *)' に一致するものが見つからない」とエラーが出ました

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: stringの文字列置換

#6

投稿記事 by みけCAT » 9年前

constを付けたデータは書き換えるべきではないですが、どうしてもというのであれば、
一旦ポインタにしてconstを外すことで、少なくともIdeone.comでは書き換えることができました。
https://ideone.com/0cv66H

しかし、やはりそんなことはするべきではないですし、
よく見るとCharsの"}"の後に";"が無く、これだけでC++のコードとして成立していないので、

コード:

code
const int Nx = 3;
const int Ny = 3;
const std::string Chars[Ny][Nx] = {
"1", "2", "3",
"4", "5", "6",
"7", "8", "9"
}
/code
という文字列を、

コード:

"a", "b", "c",
"d", "e", "f",
"g", "h", "i"
に置換したい、ということですか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: stringの文字列置換

#7

投稿記事 by みけCAT » 9年前

std::string さんが書きました:目的としましては、簡単に言ってしまえばスマホなどで見られるキーボード画面を再現したいのです。
[ひらがな]→[英字]→[数字]→[ひらがな]と、例えばスペースキーを一度押すごとに切り替えられる機能が欲しいです。
それなら、わざわざ配列を書き換えなくても、素直に多次元配列でデータを持っておけばいいのではないのですか?

例:

コード:

const int Npage = 2;
const int Nx = 3;
const int Ny = 3;
const std::string Chars[Npage][Ny][Nx] = {
	{
		"1", "2", "3",
		"4", "5", "6",
		"7", "8", "9"
	},{
		"a", "b", "c",
		"d", "e", "f",
		"g", "h", "i"
	}
};
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

std::string

Re: stringの文字列置換

#8

投稿記事 by std::string » 9年前

>>みけCAT
その通りです!

コード:

#include <iostream>  //入出力関連ヘッダ
#include <string>    //文字列関連ヘッダ
#include <Windows.h> //windowsAPI使用ヘッダ

int main()
{
	const int Nx = 13; //50音表列
	const int Ny = 5;  //50音表行

	const std::string Chars[Ny][Nx] = {
		"あ", "か", "さ", "た", "な", "は", "ま", "や", "ゃ", "ら", "わ","ぁ", " ",
		"い", "き", "し", "ち", "に", "ひ", "み", "  ", " ", "り", "を","ぃ", " ",
		"う", "く", "す", "つ", "ぬ", "ふ", "む", "ゆ", "ゅ", "る", "ん","ぅ", " ",
		"え", "け", "せ", "て", "ね", "へ", "め", "  ", " ", "れ", "゛","ぇ", " ",
		"お", "こ", "そ", "と", "の", "ほ", "も", "よ", "ょ", "ろ", "゜","ぉ", " "
	};
	
	//const int Lx = 13;
	//const int Ly = 5;
	//const std::string Chars[Ly][Lx] = {
	//	"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
	//	"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
	//	"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
	//	"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
	//	"(", ")", "'", "@", "/", " ", ".", ",", "-", "!", "?", "<", ">"
	//};

	int PosX = 0;
	int PosY = 0;
	std::string CurrStr;

	bool bRedraw = true; //審議判定
	while (1)
	{
		if (bRedraw)
		{
			bRedraw = false;

			system("cls");
			for (int y = 0; y<Ny; y++)
			{
				for (int x = 0; x<Nx; x++)
				{
					if (x == PosX && y == PosY)
					{
						std::cout << '[' << Chars[y][x] << ']';
					}
					else
					{
						std::cout << ' ' << Chars[y][x] << ' ';
					}
				}
				std::cout << std::endl;
			}
			std::cout << std::endl << CurrStr;
		}

		//key
		if (GetAsyncKeyState(VK_ESCAPE))break;

		if (GetAsyncKeyState(VK_UP))
		{
			PosY = (PosY>0 ? PosY - 1 : Ny - 1); bRedraw = true;
		}
		else if (GetAsyncKeyState(VK_DOWN))
		{
			PosY = (PosY<Ny - 1 ? PosY + 1 : 0); bRedraw = true;
		}
		else if (GetAsyncKeyState(VK_LEFT))
		{
			PosX = (PosX>0 ? PosX - 1 : Nx - 1);  bRedraw = true;
		}
		else if (GetAsyncKeyState(VK_RIGHT))
		{
			PosX = (PosX<Nx - 1 ? PosX + 1 : 0); bRedraw = true;
		}
		
		else if (GetAsyncKeyState(VK_RETURN))
		{
			CurrStr.push_back('\n'); bRedraw = true;
		}
		
		else if (GetAsyncKeyState(VK_BACK))
		{
			CurrStr = CurrStr.erase(CurrStr.size()-2); bRedraw = true;
		}
		else if (GetAsyncKeyState(VK_SPACE))
		{
			CurrStr += Chars[PosY][PosX]; bRedraw = true;
		}
		
		
		
		Sleep(50);
	}

	//
	return 0;
}

このプログラムに先程述べた切り替え機能を追加したいのです

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: stringの文字列置換

#9

投稿記事 by みけCAT » 9年前

std::string さんが書きました:このプログラムに先程述べた切り替え機能を追加したいのです
素直に追加すればいいと思います。

コード:

#include <iostream>  //入出力関連ヘッダ
#include <string>    //文字列関連ヘッダ
#include <Windows.h> //windowsAPI使用ヘッダ

int main()
{
	const int Npage = 2; // 50音表ページ
	const int Nx = 13; //50音表列
	const int Ny = 5;  //50音表行

	const std::string Chars[Npage][Ny][Nx] = {
		{
			"あ", "か", "さ", "た", "な", "は", "ま", "や", "ゃ", "ら", "わ","ぁ", " ",
			"い", "き", "し", "ち", "に", "ひ", "み", "  ", " ", "り", "を","ぃ", " ",
			"う", "く", "す", "つ", "ぬ", "ふ", "む", "ゆ", "ゅ", "る", "ん","ぅ", " ",
			"え", "け", "せ", "て", "ね", "へ", "め", "  ", " ", "れ", "゛","ぇ", " ",
			"お", "こ", "そ", "と", "の", "ほ", "も", "よ", "ょ", "ろ", "゜","ぉ", " "
		},{
			"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
			"N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
			"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
			"n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
			"(", ")", "'", "@", "/", " ", ".", ",", "-", "!", "?", "<", ">"
		}
	};

	int PosPage = 0;
	int PosX = 0;
	int PosY = 0;
	std::string CurrStr;

	bool bRedraw = true; //審議判定
	while (1)
	{
		if (bRedraw)
		{
			bRedraw = false;

			system("cls");
			for (int y = 0; y<Ny; y++)
			{
				for (int x = 0; x<Nx; x++)
				{
					if (x == PosX && y == PosY)
					{
						std::cout << '[' << Chars[PosPage][y][x] << ']';
					}
					else
					{
						std::cout << ' ' << Chars[PosPage][y][x] << ' ';
					}
				}
				std::cout << std::endl;
			}
			std::cout << std::endl << CurrStr;
		}

		//key
		if (GetAsyncKeyState(VK_ESCAPE))break;

		if (GetAsyncKeyState(VK_UP))
		{
			PosY = (PosY>0 ? PosY - 1 : Ny - 1); bRedraw = true;
		}
		else if (GetAsyncKeyState(VK_DOWN))
		{
			PosY = (PosY<Ny - 1 ? PosY + 1 : 0); bRedraw = true;
		}
		else if (GetAsyncKeyState(VK_LEFT))
		{
			PosX = (PosX>0 ? PosX - 1 : Nx - 1);  bRedraw = true;
		}
		else if (GetAsyncKeyState(VK_RIGHT))
		{
			PosX = (PosX<Nx - 1 ? PosX + 1 : 0); bRedraw = true;
		}
		
		else if (GetAsyncKeyState(VK_RETURN))
		{
			CurrStr.push_back('\n'); bRedraw = true;
		}
		
		else if (GetAsyncKeyState(VK_BACK))
		{
			if (CurrStr.size() >= 2) // そのままだとstd::out_of_rangeがthrowされたので追加
			CurrStr = CurrStr.erase(CurrStr.size()-2); bRedraw = true;
		}
		else if (GetAsyncKeyState(VK_SPACE))
		{
			CurrStr += Chars[PosPage][PosY][PosX]; PosPage = (PosPage + 1) % Npage; bRedraw = true;
		}
		
		
		
		Sleep(50);
	}

	//
	return 0;
}
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

std::string

Re: stringの文字列置換

#10

投稿記事 by std::string » 9年前

>>みけCATさん

まさにこの機能です!ありがとうございました。
文字列置換という言葉にばかりとらわれていて検討もつきませんでした。
ありがとうございます。

閉鎖

“C言語何でも質問掲示板” へ戻る